Array.prototype.forEach()

1.语法

  • forEach()与map()差不多,但是前者是没有返回值的

2.手写

Array.prototype.forEach = function(callback,thisValue) {
  if(this == undefined) {
    throw new TypeError("this is null or not undefined!!")
  }
  if(Object.prototype.toString.call(callback) != "[object Function]") {
    throw new TypeError(callback + "is not a function!")
  }
  let _this = thisValue? thisValue : this
  for(let i = 0;i < this.length;i++) {
    callback.call(_this,this[i],i,this)
  }
}

你可能感兴趣的:(Array.prototype.forEach())