Array.prototype.reduce = function (callback, initialValue) {
if (this === null || this === undefined) {
throw new TypeError('Cannot read preperty "reduce" of null or undefined')
}
if (Object.prototype.toString.call(callback) != '[object Function]') {
throw new TypeError(callback + 'is not a fuction')
}
let O = Object(this)
let len = O.length >>> 0
let k = 0
let accumulator = initialValue
if (accumulator === undefined) {
for (; k < len; k++) {
if (k in O) {
accumulator = O[k]
k++
break
}
}
throw new Error('Each element of the array is empty')
}
for (; k < len; k++) {
if (k in O) {
accumulator = callback.call(undefined, accumulator, O[k], O)
}
}
return accumulator
}
const a = [1, 2, 3]
a.reduce((per, cur, index) => {
console.log(per, cur, index)
}, [])
console