偏函数

    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
好记性不如烂笔头,看到自己觉得应该记录的知识点,结合自己的理解进行记录,用于以后回顾。
原文地址:https://www.cnblogs.com/wangxi01/p/11087143.html