3 ms·
The way functions work in JavaScript is awesome. Just think of a function as another value a variable can have. This might be more clear if we wrote it as:
by tiglionabbit 13y ago
The way functions work in JavaScript is awesome. Just think of a function as another value a variable can have. This might be more clear if we wrote it as:
var g = function(x){
var f = function(y){
return x + y;
}
return f;
}
Also because of the magic of Lexical Scoping, the new function f holds onto a reference to the variable x that it saw when it was created. If you run var myfunction = g(5), you effectively get this:
var myfunction = function(y) { return 5 + y }