1. Block-level context “generated during code execution” in ES6

Except for the curly braces for “functions and objects”

2. Block level context interpretation:

“For example: judgment body/loop body/code block…” Let /const/function /class (const/function /class); Its parent context is its environment;

  1. Let /const/function/class generates and is bound by block-level context
  2. Var does not occur and is not affected by block-level context
  3. Block-level private contexts can also be nested for variables that are not accessible outside the block-level private context

Block level private context is generated when a block number in braces is let/const/function/class

EC(Block)

VO(Block) Block-level private variable object

Actions taken before code execution

1. Initialize the scope chain

>> <EC(Block),<EC(G)>>

2. Variable promotion

Initially function only declared that the first thing that entered the block-level context was “variable promotion: declaration + assignment”

console.log(a,b,c); //=> undefined {console.log(a,b,c); //=> undefined constant let a = 12; const b = 13; var c = 14; console.log(a,b,c); //12 13 14 } console.log(a,b,c); // Error 14Copy the code

3. The particularity of function in block-level context

{differences between the new version and the old version}

Old version:

There is no block-level context, so function in braces (initial function and outside object) is left as it is, and the variable promotion phase is declared + defined;

The new version:

To be compatible with ES5 and ES6, declare it globally and in the private block-level context.Copy the code
    1. Function appears in braces other than function/object, and only function is declared in the initial variable promotion phase
    1. The block-level context is generated, and the first thing that enters the block-level context is “variable promotion: declaration + assignment” function fn={}

4. Block-level context in the body of the loop

Features of var loops

The variable is promoted so that I and item are global

/* * EC(G) * I * arr * item * var I; var arr; var item; Var does not generate block-level private context */ var arr = [10, 20, 30]; for (var i = 0; i < arr.length; Var item= arr[I]; var item= arr[I]; } console.log(i, item); //3 30 // I is global and I item is globalCopy the code

Let loop

The body of the loop produces a block-level context

Let I = 0; let I = 0; i < arr.length; I++ is used to control the loop +EC(BlockParent) +VO(BlockParent) private variables: I 0, 1, 2, 3(end of loop) every turn of the loop produces a new block-level private context @1 and every private block-level context, there's a private variable "I" that stores the value of the current loop I @2 private variable "I" and the parent of the private variable "I" gives the private variable "I" at the end of each turn, It synchronizes the value of the current private block to the parent block.Copy the code
EC (G) / * * * arr - > 0 x000 * variables: - no * / let arr = [10, 20, 30]; for (let i = 0; i < arr.length; i++) { let item = arr[i]; } console.log(I, item);} console.log(I, item); Uncaught ReferenceError: I is not defined I /item is not a global variableCopy the code