这里的示例代码运行传参多一个,且不能达到预期效果。

建议修改成:
const currying = function (fn, ...args) {
const len = fn.length;
args = args || [];
return (...arguments) => {
const totalArgs = [...args].concat([...arguments]);
return totalArgs.length >= len
? fn.call(this, ...totalArgs)
: currying.call(this, fn, ...totalArgs);
};
};
const sum = (a, b, c) => a + b + c;
const newSum = currying(sum);
const res = newSum(1)(2)(3) // 6
console.log(res);
运行结果:
