day2:js数组方法map的实现

42 阅读1分钟
Array.prototype._map = function(cb) {
  if (typeof cb !== 'function') {
    throw new Error(null);
  }
  const that = [...this];
  for (let i = 0, len = that.length; i < len; i++) {
    const value = cb(that[i], i, that);
    that[i] = value;
  }
  return that;
}

const arr = [1, 2, 3];
const res = arr._map((item)=>{
  return item*3;
})
console.log(arr, res)