方法一

  1. let _map=function(a,f){
  2. return a.reduce((sum,value,idx,a)=>{
  3. sum.push(f(value,idx,a));
  4. return sum;
  5. },[]);
  6. }
  7. let a=[1,2,3];
  8. let b=_map(a,value=>value*value);
  9. console.log('rlt:',b);

方法二

  1. Array.prototype._map=function(f,thisArg){
  2. let self = this || thisArg;
  3. console.log(thisArg);
  4. return self.reduce((sum,value,idx,self)=>{
  5. sum.push(f.bind(self)(value,idx,self));
  6. return sum;
  7. },[]);
  8. }
  9. let a=[1,2,3];
  10. let b=a._map(value=>value*value);
  11. console.log('rlt:',b);