So I'm new to programming and I'm trying to learn JS with the book Eloquent Javascript.
So far so good, until I reached an example with the following code
function makeAddFunction(amount) {
function add(number) {
return number + amount;
}
return add;
}
var addTwo = makeAddFunction(2);
var addFive = makeAddFunction(5);
show(addTwo(1) + addFive(1));
note: show is like alert, only it shows the variables on the screen of a JS console the tutorial has integrated.
The author says this is an example to show how lexical scoping allows to synthesise functions. Chapter here
What I don't understand is how addTwo
and addFive
, which supposedly are variables, can send parameters to the functions makeAddFunction
and add
, and more specifically, how does the function add
knows that the parameter the variables are sending is the parameter number
.
Thanks for your help guys!