原型方式

Array.prototype.myMap= function(cb,thisArg){
let arr= []
for(let i=0; i<this.length; i++){
arr.push(cb.call(thisArg,this[i],i,this));
}
return arr
}
Array.prototype.myMap= function(cb,thisArg){
let arr= []
this.forEach((i,idx,item)=>{
arr.push(cb.call(thisArg,this[i],i,this));
})
return arr
}
函数模式

function map(cb,arr,thisArg){
var res = [];
for(var i=0;i<arr.length;i++){
res.push(cb.call(thisArg,this[i],i,this));
}
return res
}
网友评论