From this documentation on closures:
function makeAdder(x) {
return function(y) {
return x + y;
};
}
var add5 = makeAdder(5);
var add10 = makeAdder(10);
console.log(add5(2)); // 7
console.log(add10(2)); // 12
I can't understand how in makeAdder(5)
the parameter is received as x
, but in add5(2)
it is y
.
I would expect it to say y is undefined
both times. Can anyone explain how it works the way it does?