实现reduce

Array.prototype.mm= function (fn, int) {
    let arr = Array.prototype.slice.call(this)
    let res
    let startIndex
    if (int === undefined){
        for (let i=0;i<arr;i++){
            if (!arr.hasOwnProperty(i)) continue
            startIndex = i;
            res = arr[i]
            break
        } 
    } else{
        res = int
    }
    for ( let i = int++ || 0; i<arr.length;i++){
        if (!arr.hasOwnProperty(i)) continue
        res = fn.call(null, res, arr[i], i, this)
    } 
    return res
}

posted on 2019-07-03 16:05  fxxk院子  阅读(38)  评论(0)    收藏  举报

导航