var e = 10;
function sum(a) {
return function(b) {
return function(c) {
return function(d) {
return a + b + c + d + e;
}
}
}
}
console.log(sum(1)(2)(3)(4)); //logs 20
I'm not quite sure how to read this. It was one of the examples in some MDN documentation. Do you think it would be possible to give a step-by-step narration of what's going on?
Here's what I think is happening:
I thought sum(1)
would return function(b)
(which returns function(c)
which returns function(d)
which returns 1 + undefined + undefined + undefined + 10
) but this is obviously wrong.