twice has a parameter because it's a function. The reason it's a function is because the multiplier function returns a function (on that code snippet you use the lamdba function syntax). In JS this is possible because functions are first-class citizens.
Also, twice is not a reference to the multiplier function. You create a new function when you return, so it's a reference to a new function. Every time you call multiplier, you create a new function.
Hope that helped ☺
twice has a parameter because it's a function. The reason it's a function is because the multiplier function returns a function (on that code snippet you use the lamdba function syntax). In JS this is possible because functions are first-class citizens.
Also, twice is not a reference to the multiplier function. You create a new function when you
return
, so it's a reference to a new function. Every time you call multiplier, you create a new function.Hope that helped ☺
Ok, so what I was missing is:
return x => x * factor;
is the same as:
return function(x) { return x * factor; };
Thank you for your excellent explanation!