改变sum传参个数,fn就能对应的单个调用几次
function curry(func) {
return function curried(...args) {
console.log(args.length, func.length);
if (args.length >= func.length) { // (1)
return func.apply(this, args);
} else {
return function pass(...args2) { // (2)
return curried.apply(this, args.concat(args2));
}
}
}
};
function sum(a, b) {
return a + b ;
}
let fn = curry(sum);
console.log(fn(1)(2) == 3)
下面是自己写的实现add(1)(2)(3)(4)(5)(6) = 21
function add (a, b, c, d, e, f) {
return (a + b + c + d + e + f)
}
const total =[]
function trans (fn) {
if (total.length ===fn.length) {
return (add(...total))
} else {
return function(arg){
total.push(arg)
return trans(add,arg)
}
}
}
var test1 = trans(add)
console.log(test1(1)(2)(3)(4)(5)(6))