When each function is called, an execution environment is created. The execution environment has its variable objects and scope chains. Through the scope chain, the contents of the upper scope can be accessed.

The concept of closures:

A closure is a function that has access to variables in the scope of another function (by “another function,” I usually mean the outer function that contains the closure function)

function outerFunction () {
var a = 1
return function () {
    console.log(a);
 }
}
 
var innerFunction = outerFunction();
innerFunction();

In this example, the anonymous function that prints a is wrapped in outerFunction and accesses the variable A in outerFunction’s scope, so it is, by definition, a closure.

Talk about function execution environments, scope chains, and variable objects(Scope and execution environment are the same concept.)

Here are two quotes from the javaScript High-level Language Program:

  1. “When a function is called, an execution context and the corresponding scope Chain are created” — closure 7.2, p. 178
  2. “Each execution environment has a variable object associated with it, where all the variables and functions defined in the environment are stored” — page 73 4.2 Execution environment and its scope

What is the relationship between execution context, scope Chain, and variable Object?

Pseudocode from Uncle Tom’s article:

ExecutionContext = { variableObject: { .... }, this: thisValue, Scope: [// Scope chain // list of all variable objects]};

So, when a function is called, an execution environment of the function is created, and this execution environment has a corresponding variable object and scope chain. The variable object of each function holds the data it owns for access and invocation within the function, including (within the execution environment) 1. Declare variables 2. Declare functions 3. Accept parameters such as:

Function foo (arg) {var variable = 'I am a variable'; Function innerFoo () {alert(" I'm an argument ")}} foo(' I'm an argument ');

At this point, the execution environment variable object will look like this:

ExecutionContext = {variableObject: {variable: 'I am the variable' innerFoo: [a reference to the function declaration innerFoo] arg: 'I am the parameter'}, this: ThisValue, Scope: [// Scope chain // list of all variable objects]};

The scope chain allows a function to access variables from its upper scope (execution environment). Here is an example:

function foo () { var a = 1; function innerFoo () { console.log(a) } innerFoo(); } foo(); / / print 1

A scope chain is simply a list of all variable objects from the inside out, starting with the current function variable object. With this list of scope chains, access to the upper scope is possible.

Closures and functions currying: Multiple layers of nested closures, this usage, is called “currying.” Closure curlization has two functions: parameter accumulation and delay call

function foo (a) { return function (b) { return function (c) { console.log(a + b + c); }}}foo(' I ')(' name ')(' Peng Hu Wan '); My name is Peng Huwan

From here, we can see the effect of parameter accumulation when closing currying. Let’s change the example above:

function foo (a) { return function (b) { return function (c) { console.log(a + b + c); }}} var foo1 = foo(' I '); Var foo2 = foo1(' name '); Foo2 (' Peng Hu Wan '); My name is Peng Huwan

As you can see, the innermost closure is not called when the outer functions foo and foo1 are called. The innermost closure is not executed until foo2 is finally obtained and foo2() is called, which is a feature of closures — delayed execution

https://www.cnblogs.com/pengh…