美文网首页
js封装map函数

js封装map函数

作者: louiebb | 来源:发表于2020-04-27 01:03 被阅读0次

原型方式

image.png
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
}

函数模式

image.png
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
}

相关文章

网友评论

      本文标题:js封装map函数

      本文链接:https://www.haomeiwen.com/subject/vuqewhtx.html