You can use a higher order function to port other functions with this behavior.
This function is often called curry, and it comes with many libraries ( lodash , for example).
curry , , . , originalFunction. , ,
Function#length , .
function curry (fn) {
return function (...args) {
if (args.length >= fn.length) {
return fn.call(this, ...args)
} else {
return curry(fn.bind(this, ...args))
}
}
}
function add (x, y) {
return x + y;
}
const curriedAdd = curry(add);
console.log(curriedAdd(1, 2));
console.log(curriedAdd(1)(2));
console.log(curriedAdd(1));