Execution context and execution context stack

1. Execution context

1.1 What is Execution Context (ES)?

When js code runs into a global environment, a function, or eval (regardless), it generates an execution context. An execution context is a set of specifications before and during execution of the global or function code.

1.2 Perform contextual procedures

The first step:

  • The variable and function expressions are declared, and the value is undpay
  • This lives and is assigned
  • The function is declared and the variable is promoted, if the function is declared internally and the variable parameters are initialized

The second part: execute the code body and assign values to some variables

1.3 VO and AO (Variable Object)

VO is the variable and function declaration in the execution context and this, AO is the variable and function declaration in the execution context stack for the current activity and this

Execute the context stack

2.1 What is an Execution Context Stack?

Since JS is single-threaded code execution is executed from the top down.

The execution context stack is an execution context stack formed when the execution context is entered. This execution context stack defines the internal execution of multiple execution contexts in order, and the execution is carried out according to the criterion of “first in, last out”.

There is the following code:
var a = 10;
function bar() {
    var b = 20;
    function fn() {
        var c = 30
    }
    fn()
}
bar()

Copy the code

Code to run, the global environment to form a global execution context, push the execution context within the stack, at this time at the context of the event the global context, the bar code to the function, a new execution context, a bar is generated into the stack, the context is the bar execution context, code to run into the function fn, new generation fn execution context into the stack, At this point, the active context becomes fn execution context, fn code execution completes, FN execution context exits the context stack, BAR execution completes, bar context pushes the context stack, until the global code execution completes.

Scope and scope chain

1. Scope

A scope specifies access to an internal variable object whose value is determined at execution time.

2. Scope chain

Scope chain is when a variable object is accessed, the search for the value of the variable will be based on the nested relationship of the scope, an ordered query, until the global scope, undefined.

Warning: The js scope is a lexical scope, and the chain of scope is defined. It is not affected by the location of the call.

1.

var a = 10;
function foo() {
    console.log(a)
}
function bar() {
    var a = 30;
    foo();
}
bar();
}
Copy the code

What is the output of the above code when executed?

2,

var a = 10;
function foo() {
    console.log(a)
    var a = 20;
}
function bar() {
    var a = 30;
    foo();
}
bar();
}
Copy the code

What about when the above code executes?

Reference: segmentfault.com/a/119000000… www.cnblogs.com/wangfupeng1… www.cnblogs.com/wangfupeng1…