add sum , :
, add(1) , sum 1.
The value add(1)(2)is a function where sumis 3.
The value add(1)(2)(3)is a function where sumis 6.
The call console.logwill receive the string value of the function, which is equal to sum.
You can also write basically the same code as this one, which can be a bit simpler:
function add(a) {
var sum = a;
function self(a) {
sum += a;
return self;
}
self.toString = function () {
return sum;
};
return self;
}
console.log(add(1)(2)(3));
(The difference is that it is toStringbound by the function add, and not every time you call the return function. The edges of the edges are what console.log(add(1))show 1instead self(a).)
Guffa source
share