SOURCE

Array.prototype.reduce = function (callbackfn, initValue) {
    // 异常处理, 和 map一样
    // 处理数组类型异常
    if (this === null || this === undefined) {
        throw new TypeError('Cannot read property "reduce" of null or undefined')
    }

    // 处理回调函数异常
    if (Object.prototype.toString.call(callbackfn) != '[object Function]') {
        throw new TypeError(callbackfn + 'is not a function')
    }

    let O = Object(this)
    let len = O.length >>> 0
    let k = 0
    let accumulator = initValue
    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 = callbackfn.call(undefined, accumulator, O[k], O)
        }
    }
    return accumulator
}

var a = [1, 2, 3]
a.reduce((prev, cur, index, array) => {
    console.log(prev, cur, index. array)
}, 0)
console 命令行工具 X clear

                    
>
console