Array.prototype.myReduce = function(fn, initialVal){
if(this === null){
throw new Error('对象为空')
}
if(typeof fn !== 'function'){
thow new Erroe('请传入函数')
}
var o = Object(this),
len = o.length >>> 0,
nextVal,
i = 0;
if(initialVal){
nextVal = initialVal;
}else{
nextVal = o[0];
i++;
}
for(; i < len; i++){
nextVal = fn(nextVal, o[i], i, o);
}
return nextVal;
}
js reduce函数简易实现
最新推荐文章于 2023-08-29 21:48:08 发布