about why you got console.log(addFunction(5)(5)) == 11 see my answer above.
And
if you want call console.log(addFunction(5)(5)) and get 10, you can use this solutions:
//other solutin
function tickF(n) {
var y = function(m) {
return f(m+n);
}, f = arguments.callee;
y.toString = y.valueOf = function () {
return n;
};
return y;
}
console.log('no memory leak', String(tickF(1)(2)(3)), String(tickF(1)(2)(3)))
answer 6, 6
or with closure, but this solutoin you cant call second time, cause total variable wasnt reset after call-chain complete:
//chain call fn()()()
var tick = (function(){
var total = 0;
function fnGenerator(arg) {
total += arg;
return arguments.callee;
}
fnGenerator.toString = function(){ return total;}
return fnGenerator;
})()
console.log('memory leak', String(tick(1)(1)), String(tick(1)(1)))
you will get answer n console: 2, 4
see all solutions here : https://github.com/miukki/es5-bind/blob/master/tick.js
numis a parameter of theaddFunction, and is local to there. Thenumin thefunwill refer to that (incremented) variable; the globalnumis shadowed.