函数柯里化原理:
const getSum = (args) => {
//参数求和方法
return args.reduce((a, b) => {
return a + b;
})
}
const foo = (...args) => {
const totalParamsSum = getSum(args); //首次调用,第一个参数,并作为总和total
const fn = (...arg2) => {
const sum2 = getSum(arg2);
return foo(totalParamsSum + sum2);
}
fn.toString = () => {
return totalParamsSum;
}
return fn; //只有接下来还有参数进行调用,就会再次执行fn,累加totalParamsSum
}
console.log(foo(1)(2)(3) == 6)
记录一下~