偏函数

    const partialFunc = (func, ...args) => {
        let placeholdsNum = 0
        return (...args2) => {
            args2.forEach(arg => {
                // 查找值为_的下标
                let index = args.findIndex(item => item === '_')
                if(index < 0) return
                // 填充
                args[index] = arg
                // 同时记录数量
                placeholdsNum++
            })
            if(placeholdsNum < args2.length){
                // 记录剩余值
                args2 = args2.slice(placeholdsNum, args2.length)
            }
            return func.apply(this, [...args, ...args2])
        }
    }

    let add = (a, b, c, d) => a + b + c + d
    let partialAdd2 = partialFunc(add, '_', 2, '_')
    console.log(partialAdd2(1, 3, 4)) //10

 

上一篇:Google 开源项目的风格指南


下一篇:Python9-进程-day36