首页 > 其他分享 >myForEach

myForEach

时间:2023-02-17 01:11:31浏览次数:24  
标签:function index myForEach len callback thisArg

Array.prototype.myForEach = function (callback, thisArg = undefined) {
    if (typeof callback !== 'function') {
        throw new TypeError(`${callback} is not a function`);
    }
    let index = 0
    const len = this.length
    while (index < len) {
        if (index in this) callback.call(thisArg, this[index], index, this)
        index++
    }
}

  

标签:function,index,myForEach,len,callback,thisArg
From: https://www.cnblogs.com/zhenjianyu/p/17128778.html

相关文章