要點:
1、初始值不傳怎麼處理
2、回調函數的參數有哪些,返回值如何處理。
Array.prototype.myReduce = function(fn, initialValue) {
var arr = Array.prototype.slice.call(this);
var res, startIndex;
res = initialValue ? initialValue : arr[0];
startIndex = initialValue ? 0 : 1;
for(var i = startIndex; i < arr.length; i++) {
res = fn.call(null, res, arr[i], i, this);
}
return res;
}
閱讀更多 超然不累於物 的文章
關鍵字: startIndex call prototype