Loading…
Everything will be ready in few seconds
- Theory
- Theory
- Comments
Closures
The code really works. What is the secret behind this strange code, and what does it really even mean?
var collectContainer = function () {
var food = 'pasta';
var eatDinner = function () {
console.log('I ate ' + food);
}
return eatDinner;
};
var schoolkid = collectContainer();
schoolkid();
// Outputs: I ate pasta.
We want the function to access its enclosing function’s local variable independently of changes to a different global variable with the same name. Moreover, we do not want to run the returned function immediately. It is the same thing with events, where we create a handler, but we don’t expect the handler to be executed immediately. In our example, we are assembling a lunchbox of food for our student so that he will be able to dine on his favorite pasta regardless of what is being served in the cafeteria.
The code from the example works as follows: We call collectContainer. → The collectContainer function returns eatDinner. → We call eatDinner.
We create a function inside another function. Thus, we create one scope inside a different scope. eatDinner has its own scope and reads the food variable. This variable is declared inside collectContainer, which has its own scope.
If the eatDinner function does not find a food variable that was created within its scope, it looks in the enclosing scope (in the collectContainer scope). There it finds the food variable, learns that its value is equal to the string 'pasta', and reads that value when it runs. It keeps access to this variable even after collectContainer returns.
But wait a minute! We output the schoolkid variable to the console, and yet we didn’t see any pasta there!
var collectContainer = function () {
var food = 'pasta';
var eatDinner = function () {
console.log('I ate ' + food);
};
return eatDinner;
};
var schoolkid = collectContainer();
console.log(schoolkid);
// Outputs: function () { console.log('I ate ' + food); }
Everything is true. The code of the function itself does not change. It is simply that the function keeps access to the environment in which it was created. Therefore, eatDinner is able “to remember” how to access food. This is a special feature of functions. They are able to remember their environment at the time they are created. This is called a closure. A closure is a function that remembers its surroundings. In other words, it is a function + access to variables outside its local scope. The function reads the current values of those variables when it runs; it does not store frozen copies of their values.
In our example, the eatDinner function together with the food variable forms a closure.
It is due to the closure that the code from the example above works. eatDinner keeps access to the local food from its enclosing scope. Therefore, it reads this variable when it is called. It is as if our Mom was reminding us to buy potatoes as we walked past the store. When this happens, we remember the potatoes and go about doing our chores. When we saw the store, we remembered that we needed to get potatoes, so we purchased them and went home.
Make sure that the closures allow the student to eat pasta from his lunchbox regardless of the food that is being served in the cafeteria.
- script.js
Thanks! We’ll fix everything at once!
Comments