7. 利用reduce实现数组的map方法

利用数组内置的reduce方法实现map方法,考察对reduce原理的掌握

查看答案

Array.prototype.myMap = function(fn,thisValue){
var res = [];
thisValue = thisValue||[];
this.reduce(function(pre,cur,index,arr){
return res.push(fn.call(thisValue,cur,index,arr));
},[]);
return res;
}

var arr = [2,3,1,5];
arr.myMap(function(item,index,arr){
console.log(item,index,arr);
})