偏函数
作者:互联网
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
标签:...,函数,index,args,let,placeholdsNum,args2 来源: https://www.cnblogs.com/wangxi01/p/11087143.html