Array.prototype.myMap = function(condiction,thisArr){
let ans = []
thisArr = thisArr || []
this.reduce((pre,curr,index) => {
ans.push(condiction.call(thisArr,curr,index))
},[])
return ans;
}
let arr = [1,2,4]
let test = arr.myMap(item => {
return item *= 2;
})
console.log(test);//[2, 4, 8]
核心代码:ans.push(condiction.call(thisArr,curr,index))
利用call改变this指向传入的数组,去执行传入的函数,ans来push通过condiction的return的数