I'm reading "Eloquent JavaScript". Chapter 3 introduces "Closure" concept and gives you a couple of examples. One of these is next one:
function multiplier(factor) {
return function(number) {
return number * factor;
};
}
var twice = multiplier(2);
console.log(twice(5));
// → 10
I think I understood the concept. If first I execute console.log(twice)
, since variable number
is undefined, what I get is [Function]
. What I don't understand is how twice(5)
works. Why local variable number
is initialized with value 5
?
Also, why if I execute console.log(multiplier(2,5))
I don't get 10 as a result?
Thanks.
A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function's scope from an inner function.
Definition of closure 1 : an act of closing : the condition of being closed closure of the eyelids business closures the closure of the factory. 2 : an often comforting or satisfying sense of finality victims needing closure also : something (such as a satisfying ending) that provides such a sense.
In JavaScript, closures are defined as inner functions that have access to variables and parameters of outer function even after the outer function has returned.
A closure is a function that accesses variables “outside” itself, in another function's scope, even after the parent function has closed. A closure is able to do this because of a saved reference to the lexical environment it was initially defined in.
Because multiplier
returns a function, so twice
is equal to the returned function, NOT the multiplier
function.
However, when multiplier
is called the factor
variable is passed and used within the returned function.
So to make it easier to understand, consider that twice
is basically:
var twice = function(number) {
return number * 2;
};
Where factor
has been replaced by the value you passed in when calling multiplier(2)
.
I think I understood the concept. If first I execute
console.log(twice)
, since variable number is undefined, what I get is[Function]
.
When you use console.log(twice)
you are not actually calling the function twice
, you are simply logging the value of it. So the output of [Function]
is not because number
is undefined, it is because you are outputting the actual function rather than the result of it.
Also, why if I execute
console.log(multiplier(2,5))
I don't get 10 as a result?
Here you are calling the multiplier
by providing 2 arguments, though you have only defined the function to accept one parameter (factor
). In javascript, this won't cause an error, but you will just get the first value mapped in factor
(factor = 2
).
Note: There are ways to still access all the supplied arguments even if you don't have parameters defined for them (here's an example)
Something that would be possible to get a result of 10
which might be of interest is to use the following code:
var result = multiplier(2)(5); // result = 10
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With