_.forEach = (array,fn)=>{
for(const value of array){
fn(value);
}
}
_.map = (array,fn)=>{
let results = [];
for(const value of array){
results.push(fn(value));
}
return results;
}
_.reduce = (array,fn,initialValue)=>{
//判断是否传入初始值
let accumlator = initialValue === undefined ? array[0] : initialValue;
if(initialValue === undefined){
for(let i = 1,len = array.length;i<len;i++){
accumlator = fn(accumlator,array[i]);
}
}else{
for(const value of array){
accumlator = fn(accumlator,value);
}
}
return accumlator;
}
let a = [1,2,3,4];
console.log(_.reduce(a,(acc,next)=>acc+next,0));//10