I’ll introduce sample code for function currying written in JavaScript (ES5/ES6).
The key point is defining a value method on function resultFn and returning the function itself with return resultFn;.
Since functions are also objects, both function resultFn and var resultFn are objects, so you can define a result method on them with resultFn.result = function () {}.
Reference: 関数もオブジェクトである - JavaScriptの関数とメソッド:CodeZine(コードジン)
function resultFn function example
function add(x) {
var sum = x;
function resultFn(y) {
sum += y;
return resultFn;
}
resultFn.result = function () {
return sum;
};
return resultFn;
}
var resultFn = function (y) {} method example
function add(x) {
var sum = x;
var resultFn = function (y) {
sum += y;
return resultFn;
};
resultFn.result = function () {
return sum;
};
return resultFn;
}
function resultFn function example
function add(x) {
let sum = x;
function resultFn(y) {
sum += y;
return resultFn;
}
resultFn.result = function () {
return sum;
};
return resultFn;
}
const resultFn = (y) => {} method example
function add(x) {
let sum = x;
const resultFn = (y) => {
sum += y;
return resultFn;
};
resultFn.result = function () {
return sum;
};
return resultFn;
}
> add(2)(3)(4).result
[Function]
> add(2)(3)(4).result()
9
That’s all from the Gemba.