代码实现原理如下代码:
// 01.在Array的原型对象上新增自定义方法myForEach
Array.prototype.myForEach = function(f) {
// this指向的是函数的调用者
for (let index = 0; index < this.length; index++) {
f(this[index], index, this);
}
}
// 02.定义数组
let arrData = [1, 2, 3, 4, 5, 65];
// 03.调用myForEach方法进行遍历
arrData.myForEach((item, index, arr) => {
console.log(item)
});