经典面试题函数柯里化: add(1)(2)(3) = 6
function currying() {
const args = Array.prototype.slice.call(arguments);
const inner = function () {
args.push(...arguments);
return inner;
};
inner.__proto__[Symbol.toPrimitive] =
inner.toString =
inner.getValue =
() => args.reduce((prev, next) => prev + next, 0);
return inner;
}
const sum = currying(1)(2)(3);
console.log(sum*1);// 6
const res = currying(1, 2, 3, 4, 5)(6, 7)(8, 9)(10);
console.log(res.getValue()); // 55
console.log(res - 1); // 54