var addTwo = makeAddFunction(2);
1 . 2 assigned as amount and attached to the scope of the function. The add internal function has access to this, and therefore keeps it โcachedโ.
So, that returns essentially function(number) { number + 2 };
var addFive = makeAddFunction(5);
2 . 5 is assigned in the same way, and function(number) { number + 5 }; returned function(number) { number + 5 }; .
show(addTwo(1) + addFive(1));
3. function( number ) {number+2} is called and 1 is passed to the function, so 2+1 returned, which is 3 .
4. function( number ){number+5} is called and 5 is passed to the function, so 5+1 returned, which is 6 .
5. 6 and 3 , so we get 9 .
6 . 9 is passed to the show function.
source share