Apprenez les fermetures JavaScript avec des exemples de code

Les fermetures sont un concept JavaScript fondamental que tout programmeur sérieux devrait connaître à fond.

Internet regorge de bonnes explications sur «ce que sont» les fermetures, mais peu d'explications approfondies sur le «pourquoi» des choses.

Je trouve que comprendre les éléments internes donne en fin de compte aux développeurs une meilleure compréhension de leurs outils, donc cet article sera consacré aux détails sur comment et pourquoi les fermetures fonctionnent comme ils le font.

J'espère que vous repartirez mieux équipé pour profiter des fermetures dans votre travail quotidien. Commençons!

Qu'est-ce qu'une fermeture?

Les fermetures sont une propriété extrêmement puissante de JavaScript (et de la plupart des langages de programmation). Tel que défini sur MDN:

Les fermetures sont des fonctions qui font référence à des variables indépendantes (libres) . En d'autres termes, la fonction définie dans la fermeture «se souvient» de l'environnement dans lequel elle a été créée .

Remarque: les variables libres sont des variables qui ne sont ni déclarées localement ni passées en paramètre.

Regardons quelques exemples:

Exemple 1:

function numberGenerator() { // Local “free” variable that ends up within the closure var num = 1; function checkNumber() { console.log(num); } num++; return checkNumber; } var number = numberGenerator(); number(); // 2

Dans l'exemple ci-dessus, la fonction numberGenerator crée une variable locale «libre» num (un nombre) et checkNumber (une fonction qui imprime num sur la console).

La fonction checkNumber n'a pas de variables locales propres - cependant, elle a accès aux variables dans la fonction externe, numberGenerator, en raison d'une fermeture.

Par conséquent, il peut utiliser la variable num déclarée dans numberGenerator pour la journaliser avec succès dans la console même après le retour de numberGenerator .

Exemple 2:

Dans cet exemple, nous démontrerons qu'une fermeture contient toutes les variables locales qui ont été déclarées à l'intérieur de la fonction englobante externe.

function sayHello() { var say = function() { console.log(hello); } // Local variable that ends up within the closure var hello = 'Hello, world!'; return say; } var sayHelloClosure = sayHello(); sayHelloClosure(); // ‘Hello, world!’

Remarquez comment la variable hello est définie après la fonction anonyme - mais peut toujours accéder à la variable hello . En effet, la variable hello a déjà été définie dans la fonction «scope» au moment de la création, la rendant disponible lorsque la fonction anonyme est finalement exécutée.

(Ne vous inquiétez pas, j'expliquerai ce que signifie «portée» plus tard dans l'article. Pour l'instant, suivez-le!)

Comprendre le haut niveau

Ces exemples illustrent à un niveau élevé les fermetures «en quoi». Le thème général est le suivant: nous avons accès aux variables définies dans la (les) fonction (s) englobante (s) même après le retour de la fonction englobante qui définit ces variables .

De toute évidence, quelque chose se passe en arrière-plan qui permet à ces variables d'être toujours accessibles longtemps après le retour de la fonction englobante qui les a définies.

Pour comprendre comment cela est possible, nous devrons aborder quelques concepts connexes - en commençant à 3000 pieds de haut et en redescendant lentement vers le pays des fermetures. Commençons par le contexte global dans lequel une fonction est exécutée, appelé «contexte d'exécution» .

Contexte d'exécution

Le contexte d'exécution est un concept abstrait utilisé par la spécification ECMAScript poursuivre l'évaluation d'exécution du code. Cela peut être le contexte global dans lequel votre code est exécuté pour la première fois ou lorsque le flux d'exécution entre dans un corps de fonction.

À tout moment, il ne peut y avoir qu'un seul contexte d'exécution en cours d'exécution. C'est pourquoi JavaScript est «à thread unique», ce qui signifie qu'une seule commande peut être traitée à la fois.

En règle générale, les navigateurs maintiennent ce contexte d'exécution à l'aide d'une «pile». Une pile est une structure de données Last In First Out (LIFO), ce qui signifie que la dernière chose que vous avez poussée sur la pile est la première chose qui en ressort. (C'est parce que nous ne pouvons insérer ou supprimer des éléments qu'en haut de la pile.)

Le contexte d'exécution actuel ou «en cours d'exécution» est toujours l'élément supérieur de la pile. Il apparaît en haut lorsque le code dans le contexte d'exécution en cours a été complètement évalué, ce qui permet à l'élément supérieur suivant de prendre le relais en tant que contexte d'exécution en cours.

De plus, ce n'est pas parce qu'un contexte d'exécution est en cours d'exécution qu'il doit finir de s'exécuter avant qu'un contexte d'exécution différent puisse s'exécuter.

Il y a des moments où le contexte d'exécution en cours est suspendu et un contexte d'exécution différent devient le contexte d'exécution en cours. Le contexte d'exécution suspendu pourrait alors à un stade ultérieur reprendre là où il s'était arrêté.

Chaque fois qu'un contexte d'exécution est remplacé par un autre comme celui-ci, un nouveau contexte d'exécution est créé et poussé sur la pile, devenant le contexte d'exécution actuel.

Pour un exemple pratique de ce concept en action dans le navigateur, consultez l'exemple ci-dessous:

var x = 10; function foo(a) { var b = 20; function bar(c) { var d = 30; return boop(x + a + b + c + d); } function boop(e) { return e * -1; } return bar; } var moar = foo(5); // Closure /* The function below executes the function bar which was returned when we executed the function foo in the line above. The function bar invokes boop, at which point bar gets suspended and boop gets push onto the top of the call stack (see the screenshot below) */ moar(15); 

Ensuite, lorsque boop revient, il est sorti de la pile et la barre est reprise:

Lorsque nous avons un tas de contextes d'exécution qui s'exécutent les uns après les autres - souvent mis en pause au milieu puis repris plus tard - nous avons besoin d'un moyen de garder une trace de l'état afin de pouvoir gérer l'ordre et l'exécution de ces contextes.

Et c'est en fait le cas. Conformément à la spécification ECMAScript, chaque contexte d'exécution a différents composants d'état qui sont utilisés pour suivre la progression du code dans chaque contexte. Ceux-ci inclus:

  • État d'évaluation du code: tout état nécessaire pour exécuter, suspendre et reprendre l'évaluation du code associé à ce contexte d'exécution
  • Fonction: L'objet fonction que le contexte d'exécution évalue (ou nul si le contexte évalué est un script ou un module )
  • Realm: A set of internal objects, an ECMAScript global environment, all of the ECMAScript code that is loaded within the scope of that global environment, and other associated state and resources
  • Lexical Environment: Used to resolve identifier references made by code within this execution context.
  • Variable Environment: Lexical Environment whose EnvironmentRecord holds bindings created by VariableStatements within this execution context.

If this sounds too confusing to you, don’t worry. Of all these variables, the Lexical Environment variable is the one that’s most interesting to us because it explicitly states that it resolves “identifier references” made by code within this execution context.

You can think of “identifiers” as variables. Since our original goal was to figure out how it’s possible for us to magically access variables even after a function (or “context”) has returned, Lexical Environment looks like something we should dig into!

Note: Technically, both Variable Environment and Lexical Environment are used to implement closures. But for simplicity’s sake, we’ll generalize it to an “Environment”. For a detailed explanation on the difference between Lexical and Variable Environment, see Dr. Alex Rauschmayer’s excellent article.

Lexical Environment

By definition:

A Lexical Environment is a specification type used to define the association of Identifiers to specific variables and functions based upon the lexical nesting structure of ECMAScript code. A Lexical Environment consists of an Environment Record and a possibly null reference to an outer Lexical Environment. Usually a Lexical Environment is associated with some specific syntactic structure of ECMAScript code such as a FunctionDeclaration, a BlockStatement, or a Catch clause of a TryStatement and a new Lexical Environment is created each time such code is evaluated. — ECMAScript-262/6.0

Let’s break this down.

  • “Used to define the association of Identifiers”:The purpose of a Lexical Environment is to manage data (i.e. identifiers) within code. In other words, it gives meaning to identifiers. For instance, if we had a line of code “console.log(x / 10)”, it’s meaningless to have a variable (or “identifier”) x without something that provides meaning for that variable. The Lexical Environments provides this meaning (or “association”) via its Environment Record (see below).
  • “Lexical Environment consists of an Environment Record”: An Environment Record is a fancy way to say that it keeps a record of all identifiers and their bindings that exist within a Lexical Environment. Every Lexical Environment has it’s own Environment Record.
  • “Lexical nesting structure”:This is the interesting part, which is basically saying that an inner environment references the outer environment that surrounds it, and that this outer environment can have its own outer environment as well. As a result, an environment can serve as the outer environment for more than one inner environment. The global environment is the only Lexical environment that does not have an outer environment. The language here is tricky, so let’s use a metaphor and think of lexical environments like layers of an onion: the global environment is the outermost layer of the onion; every subsequent layer below is nested within.

Abstractly, the environment looks like this in pseudocode:

LexicalEnvironment = { EnvironmentRecord: { // Identifier bindings go here }, // Reference to the outer environment outer:  };
  • “A new Lexical Environment is created each time such code is evaluated”: Each time an enclosing outer function is called, a new lexical environment is created. This is important — we’ll come back to this point again at the end. (Side note: a function is not the only way to create a Lexical Environment. Others include a block statement or a catch clause. For simplicity’s sake, I’ll focus on environment created by functions throughout this post)

In short, every execution context has a Lexical Environment. This Lexical environments holds variables and their associated values, and also has a reference to its outer environment.

The Lexical Environment can be the global environment, a module environment (which contains the bindings for the top level declarations of a Module), or a function environment (environment created due to the invocation of a function).

Scope Chain

Based on the above definition, we know that an environment has access to its parent’s environment, and its parent environment has access to its parent environment, and so on. This set of identifiers that each environment has access to is called “scope.” We can nest scopes into a hierarchical chain of environments known as the “scope chain”.

Let’s look at an example of this nesting structure:

var x = 10; function foo() { var y = 20; // free variable function bar() { var z = 15; // free variable return x + y + z; } return bar; }

As you can see, bar is nested within foo. To help you visualize the nesting, see the diagram below:

We’ll revisit this example later in the post.

This scope chain, or chain of environments associated with a function, is saved to the function object at the time of its creation. In other words, it’s defined statically by location within the source code. (This is also known as “lexical scoping”.)

Let’s take a quick detour to understand the difference between “dynamic scope” and “static scope”, which will help clarify why static scope (or lexical scope) is necessary in order to have closures.

Detour: Dynamic Scope vs. Static Scope

Dynamic scoped languages have “stack-based implementations”, meaning that the local variables and arguments of functions are stored on a stack. Therefore, the runtime state of the program stack determines what variable you are referring to.

On the other hand, static scope is when the variables referenced in a context are recorded at the time of creation. In other words, the structure of the program source code determines what variables you are referring to.

At this point, you might be wondering how dynamic scope and static scope are different. Here’s two examples to help illustrate:

Example 1:

var x = 10; function foo() { var y = x + 5; return y; } function bar() { var x = 2; return foo(); } function main() { foo(); // Static scope: 15; Dynamic scope: 15 bar(); // Static scope: 15; Dynamic scope: 7 return 0; }

We see above that the static scope and dynamic scope return different values when the function bar is invoked.

With static scope, the return value of bar is based on the value of x at the time of foo’s creation. This is because of the static and lexical structure of the source code, which results in x being 10 and the result being 15.

Dynamic scope, on the other hand, gives us a stack of variable definitions tracked at runtime — such that which x we use depends on what exactly is in scope and has been defined dynamically at runtime. Running the function bar pushes x = 2 onto the top of the stack, making foo return 7.

Example 2:

var myVar = 100; function foo() { console.log(myVar); } foo(); // Static scope: 100; Dynamic scope: 100 (function () { var myVar = 50; foo(); // Static scope: 100; Dynamic scope: 50 })(); // Higher-order function (function (arg) { var myVar = 1500; arg(); // Static scope: 100; Dynamic scope: 1500 })(foo);

Similarly, in the dynamic scope example above the variable myVar is resolved using the value of myVar at the place where the function is called. Static scope, on the other hand, resolves myVar to the variable that was saved in the scope of the two IIFE functions at creation.

As you can see, dynamic scope often leads to some ambiguity. It’s not exactly made clear which scope the free variable will be resolved from.

Closures

Some of that may strike you as off-topic, but we’ve actually covered everything we need to know to understand closures:

Every function has an execution context, which comprises of an environment that gives meaning to the variables within that function and a reference to its parent’s environment. A reference to the parent’s environment makes all variables in the parent scope available for all inner functions, regardless of whether the inner function(s) are invoked outside or inside the scope in which they were created.

So, it appears as if the function “remembers” this environment (or scope) because the function literally has a reference to the environment (and the variables defined in that environment)!

Coming back to the nested structure example:

var x = 10; function foo() { var y = 20; // free variable function bar() { var z = 15; // free variable return x + y + z; } return bar; } var test = foo(); test(); // 45

Based on our understanding of how environments work, we can say that the environment definitions for the above example look something like this (note, this is purely pseudocode):

GlobalEnvironment = { EnvironmentRecord: { // built-in identifiers Array: '', Object: '', // etc.. // custom identifiers x: 10 }, outer: null }; fooEnvironment = { EnvironmentRecord: { y: 20, bar: '' } outer: GlobalEnvironment }; barEnvironment = { EnvironmentRecord: { z: 15 } outer: fooEnvironment };

When we invoke the function test, we get 45, which is the return value from invoking the function bar (because foo returned bar). bar has access to the free variable y even after the function foo has returned because bar has a reference to y through its outer environment, which is foo’s environment! bar also has access to the global variable x because foo’s environment has access to the global environment. This is called “scope-chain lookup.”

Returning to our discussion of dynamic scope vs static scope: for closures to be implemented, we can’t use dynamic scoping via a dynamic stack to store our variables.

The reason is because it would mean that when a function returns, the variables would be popped off the stack and no longer available — which contradicts our initial definition of a closure.

What happens instead is that the closure data of the parent context is saved in what’s known as the “heap,” which allows for the data to persist after the function call that made them returns (i.e. even after the execution context is popped off the execution call stack).

Make sense? Good! Now that we understand the internals on an abstract level, let’s look at a couple more examples:

Example 1:

One canonical example/mistake is when there’s a for-loop and we try to associate the counter variable in the for-loop with some function in the for-loop:

var result = []; for (var i = 0; i < 5; i++) { result[i] = function () { console.log(i); }; } result[0](); // 5, expected 0 result[1](); // 5, expected 1 result[2](); // 5, expected 2 result[3](); // 5, expected 3 result[4](); // 5, expected 4

Going back to what we just learned, it becomes super easy to spot the mistake here! Abstractly, here’s what the environment looks like this by the time the for-loop exits:

environment: { EnvironmentRecord: { result: [...], i: 5 }, outer: null, }

The incorrect assumption here was that the scope is different for all five functions within the result array. Instead, what’s actually happening is that the environment (or context/scope) is the same for all five functions within the result array. Therefore, every time the variable i is incremented, it updates scope — which is shared by all the functions. That’s why any of the 5 functions trying to access i returns 5 (i is equal to 5 when the for-loop exits).

One way to fix this is to create an additional enclosing context for each function so that they each get their own execution context/scope:

var result = []; for (var i = 0; i < 5; i++) { result[i] = (function inner(x) { // additional enclosing context return function() { console.log(x); } })(i); } result[0](); // 0, expected 0 result[1](); // 1, expected 1 result[2](); // 2, expected 2 result[3](); // 3, expected 3 result[4](); // 4, expected 4

Yay! That fixed it :)

Another, rather clever approach is to use let instead of var, since let is block-scoped and so a new identifier binding is created for each iteration in the for-loop:

var result = []; for (let i = 0; i < 5; i++) { result[i] = function () { console.log(i); }; } result[0](); // 0, expected 0 result[1](); // 1, expected 1 result[2](); // 2, expected 2 result[3](); // 3, expected 3 result[4](); // 4, expected 4

Tada! :)

Example 2:

In this example, we’ll show how each call to a function creates a new separate closure:

function iCantThinkOfAName(num, obj) { // This array variable, along with the 2 parameters passed in, // are 'captured' by the nested function 'doSomething' var array = [1, 2, 3]; function doSomething(i) { num += i; array.push(num); console.log('num: ' + num); console.log('array: ' + array); console.log('obj.value: ' + obj.value); } return doSomething; } var referenceObject = { value: 10 }; var foo = iCantThinkOfAName(2, referenceObject); // closure #1 var bar = iCantThinkOfAName(6, referenceObject); // closure #2 foo(2); /* num: 4 array: 1,2,3,4 obj.value: 10 */ bar(2); /* num: 8 array: 1,2,3,8 obj.value: 10 */ referenceObject.value++; foo(4); /* num: 8 array: 1,2,3,4,8 obj.value: 11 */ bar(4); /* num: 12 array: 1,2,3,8,12 obj.value: 11 */

In this example, we can see that each call to the function iCantThinkOfAName creates a new closure, namely foo and bar. Subsequent invocations to either closure functions updates the closure variables within that closure itself, demonstrating that the variables in each closure continue to be usable by iCantThinkOfAName’s doSomething function long after iCantThinkOfAName returns.

Example 3:

function mysteriousCalculator(a, b) { var mysteriousVariable = 3; return { add: function() { var result = a + b + mysteriousVariable; return toFixedTwoPlaces(result); }, subtract: function() { var result = a - b - mysteriousVariable; return toFixedTwoPlaces(result); } } } function toFixedTwoPlaces(value) { return value.toFixed(2); } var myCalculator = mysteriousCalculator(10.01, 2.01); myCalculator.add() // 15.02 myCalculator.subtract() // 5.00

What we can observe is that mysteriousCalculator is in the global scope, and it returns two functions. Abstractly, the environments for the example above look like this:

GlobalEnvironment = { EnvironmentRecord: { // built-in identifiers Array: '', Object: '', // etc... // custom identifiers mysteriousCalculator: '', toFixedTwoPlaces: '', }, outer: null, }; mysteriousCalculatorEnvironment = { EnvironmentRecord: { a: 10.01, b: 2.01, mysteriousVariable: 3, } outer: GlobalEnvironment, }; addEnvironment = { EnvironmentRecord: { result: 15.02 } outer: mysteriousCalculatorEnvironment, }; subtractEnvironment = { EnvironmentRecord: { result: 5.00 } outer: mysteriousCalculatorEnvironment, };

Because our add and subtract functions have a reference to the mysteriousCalculator function environment, they’re able to make use of the variables in that environment to calculate the result.

Example 4:

One final example to demonstrate an important use of closures: to maintain a private reference to a variable in the outer scope.

function secretPassword() { var password = 'xh38sk'; return { guessPassword: function(guess) { if (guess === password) { return true; } else { return false; } } } } var passwordGame = secretPassword(); passwordGame.guessPassword('heyisthisit?'); // false passwordGame.guessPassword('xh38sk'); // true

This is a very powerful technique — it gives the closure function guessPassword exclusive access to the password variable, while making it impossible to access the password from the outside.

TL;DR

  • Execution context is an abstract concept used by the ECMAScript specification totrack the runtime evaluation of code. At any point in time, there can only be one execution context that is executing code.
  • Every execution context has a Lexical Environment. This Lexical environments holds identifier bindings (i.e. variables and their associated values), and also has a reference to its outer environment.
  • The set of identifiers that each environment has access to is called “scope.” We can nest these scopes into a hierarchical chain of environments, known as the “scope chain”.
  • Every function has an execution context, which comprises of a Lexical Environment that gives meaning to the variables within that function and a reference to its parent’s environment. And so it appears as if the function “remembers” this environment (or scope) because the function literally has a reference to this environment. This is a closure.
  • A closure is created every time an enclosing outer function is called. In other words, the inner function does not need to return for a closure to be created.
  • The scope of a closure in JavaScript is lexical, meaning it’s defined statically by its location within the source code.
  • Closures have many practical use cases. One important use case is to maintain a private reference to a variable in the outer scope.

Clos(ure)ing remarks

I hope this post was helpful and gave you a mental model for how closures are implemented in JavaScript. As you can see, understanding the nuts and bolts of how they work makes it much easier to spot closures — not to mention saving a lot of headache when it’s time to debug.

PS: I’m human and make mistakes — so if you find any mistakes I’d love for you to let me know!

Further Reading

For the sake of brevity I left out a few topics that might be interesting to some readers. Here are some links that I wanted to share:

  • What’s the VariableEnvironment within an execution context? Dr. Axel Rauschmayer does a phenomenol job explaining it so I’ll leave you off with a link to his blog post: //www.2ality.com/2011/04/ecmascript-5-spec-lexicalenvironment.html
  • What are the different types of Environment Records? Read the spec here: //www.ecma-international.org/ecma-262/6.0/#sec-environment-records
  • Excellent article by MDN on closures: //developer.mozilla.org/en-US/docs/Web/JavaScript/Closures
  • Others? Please suggest and I’ll add them!