const __ = Symbol('PLACEHOLDER');
function placehoder(fn, ...bound){
return function newFn(...args){
// 先按函数的参数长度补齐占位
while(bound.length < fn.length){
bound.push(__)
}
// 替换参数
let j = 0;
for(let i = 0; i < fn.length; i++){
if(bound[i] === __){
if(j < args.length){
bound[i] = args[j]
j++
}
}
}
// 没有占位符了执行
if(bound.indexOf(__) == -1 && bound.length >= fn.length){
// console.log(bound)
return fn.apply(this, bound)
}else{
// 返回一个方法接收新的参数
return (...args1)=>{
return newFn.apply(this, args1)
}
}
}
}
function dot(x1, y1, x2, y2){
return x1 * x2 + y1 * y2
}
console.log(placehoder(dot, 10, __ , 10, __)(2, __)(3))
console.log(dot(10, 2, 10, 3))
console.log(placehoder(dot, __, __ , __, 5)(4, __, 2)(__)(3))
console.log(dot(4, 3, 2, 5))
console.log(placehoder(dot,3)(__, __)(4, __, 2)(3))
console.log(dot(3, 4, 3, 2))
console.log(placehoder(dot)(3, __, __, 4)(5, 6))
console.log(dot(3, 5, 6, 4))
console.log(placehoder(dot, 3,4, 5, 3)())
console.log(dot(3, 4, 5, 3))
console