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. Здесь глава
Я не понимаю, как addTwo
и addFive
, которые предположительно являются переменными, могут отправлять параметры в функции makeAddFunction
и add
, а более конкретно, как функция add
знает, что параметр, который отправляют переменные, является параметром номер
.
Спасибо за вашу помощь ребята!