1.源码
Array.prototype.jlMap= function(fn,thisArg){
if(typeof fn !== 'function'){
throw new TypeError('type is error');
}
let array =this;
const resArr=[];
if(thisArg){
array.forEach((currentValue,index)=>{
const resault= fn.call(thisArg,currentValue,index,array);
if(!resault){
throw new Error(' it is should behave a returned value ');
}
resArr.push(resault);
})
}
if(!thisArg){
array.forEach((currentValue,index)=>{
const resault= fn(currentValue,index,array);
if(!resault){
throw new Error(' it is should behave a returned value ');
}
resArr.push(resault);
})
}
return resArr;
}
let test = [1,12,2,34,4,5,5,66,7];
try{
let q = test.jlMap( function(item){
console.log(item*2);
console.log(this)
return item*2;
},{arg:'456'}
);
console.log(q)
}
catch(err){
console.log(err)
}
如果对你有帮助,请点个赞吧
|