javascript - function example in text does not make sense to me -
reading through eloquent javascript in attempt wrap head around functions, read example 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));
i gist of it, having examined code , read accompanying text several times on few hours, hasn't clicked me: exacly code doing? add
function acquire number
parameter? come show
command? if so, how passed around? don't see it...
the function makeaddfunction
takes number (amount
) , returns function adds number passed in paramter number passed outer function.
function makeaddfunction(amount) { function add(number) { return number + amount; } return add; }
calling var addtwo = makeaddfunction(2);
equivalent writing following function:
var addtwo = function(number) { return number + 2; // function returned makeaddfunction }
you can call parameter addtwo(5);
here's fiddle: http://jsfiddle.net/eh4lk/1/ press run button execute
Comments
Post a Comment