My best advice is you try to learn a bit about Javascript closures. Really. I might not be the answer you are looking for, but it is the best you can do if you want to understand what's happening there.
Get yourself a copy of any good javascript book. Let me suggest 'Javascript - The Good Parts' by Douglas Crockford.
For some of us, Javascript closures were not something we grokked. I hope it's easier for you.
Anyway, makeAddFunction
is a function creator. It creates new functions which are tied to the parameter you passed to makeAddFunction
. Therefore, the addTwo
variable receives and stores a new function, which you can invoke later by appending parentheses to it, i.e. addTwo()
.
The parameter you pass to addTwo
, i.e. 1
on invokation addTwo(1)
is passed to the add
function, because addTwo
is nothing more than an add
function where the amount
var has a fix value of 2
.