How Do JavaScript Closures Work

How do JavaScript closures work?

A closure is a pairing of:

  1. A function and
  2. A reference to that function's outer scope (lexical environment)

A lexical environment is part of every execution context (stack frame) and is a map between identifiers (i.e. local variable names) and values.

Every function in JavaScript maintains a reference to its outer lexical environment. This reference is used to configure the execution context created when a function is invoked. This reference enables code inside the function to "see" variables declared outside the function, regardless of when and where the function is called.

If a function was called by a function, which in turn was called by another function, then a chain of references to outer lexical environments is created. This chain is called the scope chain.

In the following code, inner forms a closure with the lexical environment of the execution context created when foo is invoked, closing over variable secret:

function foo() {
const secret = Math.trunc(Math.random() * 100)
return function inner() {
console.log(`The secret number is ${secret}.`)
}
}
const f = foo() // `secret` is not directly accessible from outside `foo`
f() // The only way to retrieve `secret`, is to invoke `f`

How do JavaScript closures work?

A closure is a pairing of:

  1. A function and
  2. A reference to that function's outer scope (lexical environment)

A lexical environment is part of every execution context (stack frame) and is a map between identifiers (i.e. local variable names) and values.

Every function in JavaScript maintains a reference to its outer lexical environment. This reference is used to configure the execution context created when a function is invoked. This reference enables code inside the function to "see" variables declared outside the function, regardless of when and where the function is called.

If a function was called by a function, which in turn was called by another function, then a chain of references to outer lexical environments is created. This chain is called the scope chain.

In the following code, inner forms a closure with the lexical environment of the execution context created when foo is invoked, closing over variable secret:

function foo() {
const secret = Math.trunc(Math.random() * 100)
return function inner() {
console.log(`The secret number is ${secret}.`)
}
}
const f = foo() // `secret` is not directly accessible from outside `foo`
f() // The only way to retrieve `secret`, is to invoke `f`

How do JavaScript closures work at a low level?

This is a section of slebetman's answer to the question javascript can't access private properties that answers your question very well.

The Stack:


A scope is related to the stack frame (in Computer Science it's called
the "activation record" but most developers familiar with C or
assembly know it better as stack frame). A scope is to a stack frame
what a class is to an object. By that I mean that where an object is
an instance of a class, a stack frame is an instance of scope.

Let's use a made-up language as an example. In this language, like in
javascript, functions define scope. Lets take a look at an example
code:

var global_var

function b {
var bb
}

function a {
var aa
b();
}

When we read the code above, we say that the variable aa is in scope
in function a and the variable bb is in scope in function b.
Note that we don't call this thing private variables. Because the
opposite of private variables are public variables and both refer to
properties bound to objects. Instead we call aa and bb local
variables. The opposite of local variables are global variables
(not public variables).

Now, let's see what happens when we call a:

a() gets called, create a new stack frame. Allocate space for local
variables on the stack:

The stack:
┌────────┐
│ var aa │ <── a's stack frame
╞════════╡
┆ ┆ <── caller's stack frame

a() calls b(), create a new stack frame. Allocate space for local
variables on the stack:

The stack:
┌────────┐
│ var bb │ <── b's stack frame
╞════════╡
│ var aa │
╞════════╡
┆ ┆

In most programming languages, and this includes javascript, a
function only has access to its own stack frame. Thus a() cannot
access local variables in b() and neither can any other function or
code in global scope access variables in a(). The only exception are
variables in global scope. From an implementation point of view this
is achieved by allocating global variables in an area of memory that
does not belong to the stack. This is generally called the heap. So to
complete the picture the memory at this point looks like this:

The stack:     The heap:
┌────────┐ ┌────────────┐
│ var bb │ │ global_var │
╞════════╡ │ │
│ var aa │ └────────────┘
╞════════╡
┆ ┆

(as a side note, you can also allocate variables on the heap inside
functions using malloc() or new)

Now b() completes and returns, it's stack frame is removed from the
stack:

The stack:     The heap:
┌────────┐ ┌────────────┐
│ var aa │ │ global_var │
╞════════╡ │ │
┆ ┆ └────────────┘

and when a() completes the same happens to its stack frame. This is
how local variables gets allocated and freed automatically - via
pushing and popping objects off the stack.

Closures:


A closure is a more advanced stack frame. But whereas normal stack
frames gets deleted once a function returns, a language with closures
will merely unlink the stack frame (or just the objects it contains)
from the stack while keeping a reference to the stack frame for as
long as it's required.

Now let's look at an example code of a language with closures:

function b {
var bb
return function {
var cc
}
}

function a {
var aa
return b()
}

Now let's see what happens if we do this:

var c = a()

First function a() is called which in turn calls b(). Stack frames
are created and pushed onto the stack:

The stack:
┌────────┐
│ var bb │
╞════════╡
│ var aa │
╞════════╡
│ var c │
┆ ┆

Function b() returns, so it's stack frame is popped off the stack.
But, function b() returns an anonymous function which captures bb
in a closure. So we pop off the stack frame but don't delete it from
memory (until all references to it has been completely garbage
collected):

The stack:             somewhere in RAM:
┌────────┐ ┌╶╶╶╶╶╶╶╶╶┐
│ var aa │ ┆ var bb ┆
╞════════╡ └╶╶╶╶╶╶╶╶╶┘
│ var c │
┆ ┆

a() now returns the function to c. So the stack frame of the call
to b() gets linked to the variable c. Note that it's the stack
frame that gets linked, not the scope. It's kind of like if you create
objects from a class it's the objects that gets assigned to variables,
not the class:

The stack:             somewhere in RAM:
┌────────┐ ┌╶╶╶╶╶╶╶╶╶┐
│ var c╶╶├╶╶╶╶╶╶╶╶╶╶╶┆ var bb ┆
╞════════╡ └╶╶╶╶╶╶╶╶╶┘
┆ ┆

Also note that since we haven't actually called the function c(),
the variable cc is not yet allocated anywhere in memory. It's
currently only a scope, not yet a stack frame until we call c().

Now what happens when we call c()? A stack frame for c() is
created as normal. But this time there is a difference:

The stack:
┌────────┬──────────┐
│ var cc var bb │ <──── attached closure
╞════════╤──────────┘
│ var c │
┆ ┆

The stack frame of b() is attached to the stack frame of c(). So
from the point of view of function c() it's stack also contains all
the variables that were created when function b() was called (Note
again, not the variables in function b() but the variables created
when function b() was called - in other words, not the scope of b()
but the stack frame created when calling b(). The implication is that
there is only one possible function b() but many calls to b() creating
many stack frames).

But the rules of local and global variables still applies. All
variables in b() become local variables to c() and nothing else.
The function that called c() has no access to them.

What this means is that when you redefine c in the caller's scope
like this:

var c = function {/* new function */}

this happens:

                     somewhere in RAM:
┌╶╶╶╶╶╶╶╶╶┐
┆ var bb ┆
└╶╶╶╶╶╶╶╶╶┘
The stack:
┌────────┐ ┌╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┐
│ var c╶╶├╶╶╶╶╶╶╶╶╶╶╶┆ /* new function */ ┆
╞════════╡ └╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶╶┘
┆ ┆

As you can see, it's impossible to regain access to the stack frame
from the call to b() since the scope that c belongs to doesn't
have access to it.

How do JavaScript closures work?

A closure is a pairing of:

  1. A function and
  2. A reference to that function's outer scope (lexical environment)

A lexical environment is part of every execution context (stack frame) and is a map between identifiers (i.e. local variable names) and values.

Every function in JavaScript maintains a reference to its outer lexical environment. This reference is used to configure the execution context created when a function is invoked. This reference enables code inside the function to "see" variables declared outside the function, regardless of when and where the function is called.

If a function was called by a function, which in turn was called by another function, then a chain of references to outer lexical environments is created. This chain is called the scope chain.

In the following code, inner forms a closure with the lexical environment of the execution context created when foo is invoked, closing over variable secret:

function foo() {
const secret = Math.trunc(Math.random() * 100)
return function inner() {
console.log(`The secret number is ${secret}.`)
}
}
const f = foo() // `secret` is not directly accessible from outside `foo`
f() // The only way to retrieve `secret`, is to invoke `f`

Where does a JavaScript closure live?

tl;dr:

where does the variable live?

In the environment it was defined in.

Does it become a property of function3, or stored somewhere else in function3?

No.

Does JavaScript traverse some kind of closure chain, similarly to how it traverses the prototype chain?

Yes.

Is it stored in memory somewhere else?

Yes.


tl;dr 2:

Functions keep a reference to the environment they are created in. When a function is called it creates a new environment whose parent is the environment the function kept the reference to.


Longer explanation:

Whenever a function is executed a new lexical environment is created. The environment has two "fields": an environment record where all the variables are being tracked and a outer lexical environment that refers to, as the name suggested, to the "parent lexical environment".

So when we your code example is evaluated, the initial state of the memory (before executing anything) might look like this (simplified):

+-(Global) lexical environment-+     +-Environment Record-+
+-------------+----------------+ +---------+----------+
| Environment | *--------+---> |function1|undefined |
| Record | | +---------+----------+
+-------------+----------------+ |function3|undefined |
| Outer | | +---------+----------+
| lexical | (empty) |
| environment | |
+-------------+----------------+

The global environment doesn't have any outer environment because it is at the top. function1 and function3 are two bindings that haven't been initialized yet (the assignment wasn't evaluated yet).

After creating the function (evaluating function1 = function() { ... }), the memory looks like this:

            +------------------------------------------------------------------------+
| |
v |
+-(Global) lexical environment-+ +-Environment Record-+ +-----Function Object-+---+
+-------------+----------------+ +---------+----------+ +---------------+-----+---+
| Environment | *--------+--->|function1| *-----+---->|[[Environment]]| * |
| Record | | +---------+----------+ +---------------+---------+
+-------------+----------------+ |function3|undefined | | name |function1|
| Outer | | +---------+----------+ +---------------+---------+
| lexical | (empty) |
| environment | |
+-------------+----------------+

Now function1 has a value, a function object. Function objects have multiple internal (e.g. [[Environment]]) and external (e.g. name) properties. As the name implies, internal properties cannot be accessed from user code. The [[Environment]] property is very important. Notice how it refers back to the lexical environment the function was created in!

The next step is executing function3 = function1(), i.e. calling function2. As I said at the very beginning, whenever a function is executed a new lexical environment is created. Let's look at the memory just after entering the function:

               +------------------------------------------------------------------------+
| |
v |
+-(Global) lexical environment-+ +-Environment Record-+ +-----Function Object-+---+
+-------------+----------------+ +---------+----------+ +---------------+-----+---+
| Environment | *--------+--->|function1| +---->|[[Environment]]| * |
| Record | | +---------+----------+ +---------------+---------+
+> +-------------+----------------+ |function3|undefined | | name |function1|
| | Outer | | +---------+----------+ +---------------+---------+
| | lexical | (empty) |
| | environment | |
| +-------------+----------------+
|
|
|
| +-----lexical environment------+ +-Environment Record-+
| +-------------+----------------+ +---------+----------+
| | Environment | *--------+--->|variable |undefined |
| | Record | | +---------+----------+
| +-------------+----------------+ |function2|undefined |
| | Outer | | +---------+----------+
| | lexical | * |
| | environment | | |
| +-------------+--------+-------+
| |
+-------------------------+

This looks very similar to the structure of the global environment! We have a lexical environment that has an environment record with two unintialized bindings. But the big difference now is that "outer lexical environment" points to the global lexical environment. How is that possible?

When calling function1 and creating a new lexical environment, we set the value of the new environments "outer lexical environment" field to the value of function1's [[Environment]] field. This is were the scope chain is created.

Now, after executing function1, the memory has this structure:

               +------------------------------------------------------------------------+
| |
v |
+-(Global) lexical environment-+ +-Environment Record-+ +-----Function Object-+---+
+-------------+----------------+ +---------+----------+ +---------------+-----+---+
| Environment | *--------+--->|function1| *-----+---->|[[Environment]]| * |
| Record | | +---------+----------+ +---------------+---------+
+> +-------------+----------------+ |function3| | | | name |function1|
| | Outer | | +---------+---+------+ +---------------+---------+
| | lexical | (empty) | |
| | environment | | |
| +-------------+----------------+ +-------------------------+
| |
| +----------------------------------------------------------------+--------+
| v | |
| +-----lexical environment------+ +-Environment Record-+ v |
| +-------------+----------------+ +---------+----------+ |
| | Environment | *--------+--->|variable | 'foo' | +-----Function Object-+---+
| | Record | | +---------+----------+ +---------------+-----+---+
| +-------------+----------------+ |function2| *-----+---->|[[Environment]]| * |
| | Outer | | +---------+----------+ +---------------+---------+
| | lexical | * | | name |function2|
| | environment | | | +---------------+---------+
| +-------------+--------+-------+
| |
+-------------------------+

Similar like function1, function2 has a reference to the environment created by calling function2. In addition, function3 refers to the function we created because we return it from function1.

Last step: calling function3('bar'):

               +------------------------------------------------------------------------+
| |
v |
+-(Global) lexical environment-+ +-Environment Record-+ +-----Function Object-+---+
+-------------+----------------+ +---------+----------+ +---------------+-----+---+
| Environment | *--------+--->|function1| *-----+---->|[[Environment]]| * |
| Record | | +---------+----------+ +---------------+---------+
+> +-------------+----------------+ |function3| | | | name |function1|
| | Outer | | +---------+---+------+ +---------------+---------+
| | lexical | (empty) | |
| | environment | | |
| +-------------+----------------+ +-------------------------+
| |
| +----------------------------------------------------------------+--------+
| v | |
| +-----lexical environment------+ +-Environment Record-+ v |
| +-------------+----------------+ +---------+----------+ |
| | Environment | *--------+--->|variable | 'foo' | +-----Function Object-+---+
| | Record | | +---------+----------+ +---------------+-----+---+
|+>+-------------+----------------+ |function2| *-----+---->|[[Environment]]| * |
|| | Outer | | +---------+----------+ +---------------+---------+
|| | lexical | * | | name |function2|
|| | environment | | | +---------------+---------+
|| +-------------+--------+-------+
++------------------------+
|
| +-----lexical environment------+ +-Environment Record-+
| +-------------+----------------+ +---------+----------+
| | Environment | *--------+--->|argument | 'bar' |
| | Record | | +---------+----------+
| +-------------+----------------+
| | Outer | |
| | lexical | * |
| | environment | | |
| +-------------+--------+-------+
+------------------------+

Similar here, a new environment is created and its "outer lexical environment" field points to the environment created when function1 was called.

Now, looking up the value of argument is straightforward, because it exists in the environment's own record. But when looking up variable, the following happens: Since it doesn't exist in the environment's own record, it looks at its "outer lexical environment"'s record. It can do that because it has a reference to it.

Closure in JavaScript?

function a(name) {
return function () {
console.log( "Hello "+ name);
}
}
var b = a("MyName");
b();

A closure is the combination of a function and the lexical environment within which that function was declared. This environment consists of any local variables that were in-scope at the time that the closure was created. In this case, b is a reference to the instance of the inner function returned when a() is run. The instance of returned function maintains a reference to its lexical environment, within which the variable name exists. For this reason, when b() is invoked, the variable name remains available for use and name is passed to console.

Javascript closures with let variable

In the first code example, you have three different i variables and in each iteration of the loop, callback function of setTimeout closes over a different copy of i variable.

In the second code example, there is only one i variable and in each iteration of the loop, callback function of setTimeout closes over the same i variable.

There is only one i variable in the second code example because you have declared it outside the loop; as a result, each iteration of the loop sees the same variable i.

In order to get the same output in the second code example, change it as shown below:

let i = 0;

for (i = 0; i < 3; i++) {

let j = i; // save a copy of the current value of 'i'

setTimeout(() => {
console.log(j); // closure over a different 'j' variable in each iteration
}, 1000);
}

how is closure working with let in js?

I believe that in javaScript, the interpreter is understanding that you are creating a closure if function keyword is used another function and been returned. in most other common languages, after a function returns, all the local variables are no longer accessible because the context is destroyed, however in JavaScript, if a function is declared within another function, then all local variables can remain accessible after returning from the function you called. In simple words, the inner function will run in it's parent scope and can access all parents variables and function.
For detailed info check this link.



Related Topics



Leave a reply



Submit