function makeAdder(x) {
return function(y) {
console.log("X:" + x + " Y:" + y);
return x + y;
};
}
var add5 = makeAdder(5);
var add10 = makeAdder(10);
console.log(add5(2));
console.log(add10(2));
Ok, I am a bit confused with this example on developer.mozilla under closures.
If someone can explain with as much detail as possible in order for me to get my head around closures.
Ignore the console.log, I just added that to see what values are displayed and from that I can see that x is 5 and y is 2 when you execute add5 for example.
I consoled add5() to see what I get and I got NaN - and I am guessing that is because I have not specified an argument because it wants one and can't add a number to undefined.
So the confusion is that argument y in madeAdder's inner function.
Hope someone can provide a much better explanation than mozilla...i think the clue is environments but I'm new to this so need help from experts.
Thanks