当前位置 博文首页 > 毛小星的博客:实现Array.prototype.filter

    毛小星的博客:实现Array.prototype.filter

    作者:[db:作者] 时间:2021-08-11 21:50

    Array.prototype.my_filter = function(fn, context) {
    
        let resArr = []
        const me = this
    
        const ctx = context ? context : this // 判断上下文
    
        if (typeof fn !== 'function') {
            throw new Error(`${fn} is not a function`)
        }
    
        me.forEach((item, index) => {
            const bool = fn.call(ctx, item, index, me) // 绑定上下文,并执行结果
            if (bool) { // 如果符合条件,就放进新的数组
                resArr.push(item)
            }
        })
    
        return resArr
    }
    

    下面来验证一下

    const arr = [1,2,3]
    
    const newArr = arr.my_filter(function(item, index, _arr) {
        console.log(this, item, index, _arr)
        return item >= 2
    })
    
    console.log(newArr)
    

    再来验证一下绑定上下文

    const arr = [1,2,3]
    const arr1 = [4,5,6]
    
    const newArr = arr.my_filter(function(item, index, _arr) {
        console.log(this, item, index, _arr)
        return item >= 2
    }, arr1)
    
    console.log(newArr)
    

    再来看一下错误验证

    const arr = [1,2,3]
    const arr1 = [4,5,6]
    
    const newArr = arr.my_filter(1, arr1)
    
    console.log(newArr)
    

    cs
    下一篇:没有了