1、回调函数必须是一个函数
2、调用该方法的对象必须是数组。
3. 数组的map方法,其中需要传入两个参数,第一个是一个回调函数,第二个是一个回调函数的this指向的值。
Array.prototype.myMap = function (fn, thisArg) {
if (Object.prototype.toString.call(fn) !== '[object Function]') throw 'Error no Function'
let resArray = []
for (let i = 0; i < this.length; i++) {
resArray[i] = fn.call(thisArg, this[i], i, this)
}
return resArray
}
const arr = [1, 2, 3, 4, 5]
console.log(arr.myMap(item => item * 2)); // [2, 4, 6, 8, 10]