//->自己在内置类的原型上扩展一个myForEach来处理forEach不兼容的问题
//callBack:回调函数,遍历数组中的一项,就要执行一次callBack
//context:改变callBack方法中的this指向

Array.prototype.myForEach = function myForEach(callBack, context) {

typeof context === "undefined" ? context = window : null;

if ("forEach" in Array.prototype) {
this.forEach(callBack, context);
return;
}

//->不兼容处理
for (var i = 0; i < this.length; i++) {
typeof callBack === "function" ? callBack.call(context, this[i], i, this) : null;
}
};





 

相关文章:

  • 2022-12-23
  • 2021-07-22
  • 2022-12-23
  • 2022-12-23
  • 2022-02-10
  • 2022-12-23
  • 2022-12-23
  • 2021-12-23
猜你喜欢
  • 2021-06-26
  • 2022-02-28
  • 2021-11-25
  • 2022-12-23
  • 2022-12-23
  • 2022-02-28
相关资源
相似解决方案