I’m reading this book: http://eloquentjavascript.net/ which I think is brilliant.
However I’m having difficulty understanding the following function, where does the function add(number) get its argument from?
function makeAddFunction(amount) {
function add(number) {
return number + amount;
}
return add;
}
var addTwo = makeAddFunction(2);
var addFive = makeAddFunction(5);
show(addTwo(1) + addFive(1)); // gives 9
I thought the answer would be 7 to this show(addTwo(1) + addFive(1));
In makeAddFunction(2), amount is 2, but what would number be? so number + 2…
NB: show function is pretty much echo in php.
See JAAulde’s answer for what
makeAddFunction‘s purpose is, which was really the main question, imoThe answer to your second question is, you generate two functions. They look like this (basically):
It should be obvious why you get:
addTwo(1) + addFive(1)(1 + 2)+(1 + 5)= 9 now.