Skip to content
js
Array.prototype.myforEach = function(callback, thisArg){
    // this: 数组
    // 数组的长度
    const length = this.length;
    for (let i = 0; i < length; i++){
        callback.call(thisArg, this[i], i, this);
    }
}

let test = [1, 2, 3];
let obj = {
	nihao: 'nihao',
}

test.myforEach(function(){
    console.log(this.nihao);
},obj);

使用 call 的时候需要注意箭头函数的特殊性。