Variable promotion and function promotion

Variable declaration enhancement: Variables defined (declared) by var can be accessed before the statement is defined, and the value is undefined.

Function declaration enhancement: Functions declared through function can be called directly before

Note: must be a function declared by function

Var myfun = function (){Copy the code

Variable promotion is performed before function promotion

Remember: function promotion takes precedence over variable promotion and is not overridden by variable declarations of the same name, but is overridden by variable assignments

Variables declared by var and function are created in the global object, while variables declared by let ‘const\class’ are created in the global scope

When a variable is searched, the variable will be searched in scope first, and then in the global object

Execution context

Before executing a statement, JavaScript goes through a series of “preparations” to create a “classroom” — the execution context — for code execution

Code classification (by location)

  1. Global code
  2. Function code is also called local code

Global execution context

Identify the window as a global execution context before executing global code

Global data is processed

1. 'var' defines global variable, ==> undefined, add as window property 2. Function declares global function ==> assign (fun), add as window method 3.Copy the code

Start executing global code

Function execution context

Before the function is called and ready to execute the function body, the function execution context object is created. The function execution context is generated during the call and released automatically after the function call.

Local data is preprocessed

Arguments ==> Assign (arguments) ==> Add attribute for execution context 2. arguments ==> Assign (argument list), add attribute for execution context 3. global variable defined by 'var' ==> undefined, Add attributes for the execution context 4.function declared global function ==> assignment (fun), add methods for the execution context 5.this ==> assignment (the object calling the function)Copy the code

Start executing the function body code

Execution context stack

Number of execution contexts =N(number of functions called)+1(window)

Before global code execution, the JS engine creates a stack to manage all generated execution context objects

<script>
  
  var a = 10;
  var bar = function(x){
    var b = 5;
    foo(x + b);
  }
  var foo = function(y){
    var c = 5;
    console.log(a + c + y)
  }
  bar(10)''
</script>
Copy the code

As shown: the current running is always at the top of the stack

Execution stack The execution context at the top of the stack is called the current execution context

JS code always runs in the current execution context

A new execution context can be created in four cases:

  1. Access global code
  2. Enter the function body code
  3. Enter the eval parameter code
  4. Enter module code

scope

It is a piece of territory that shows the area in which a piece of code resides, and unlike the execution context, it is static and determined as we write the code. The function is to isolate variables. Variables with the same name in different scopes do not conflict.

Global scope

Function scope

Block scope (after ES6)

Number of scopes =N(number of declared functions)+1(window)

The scope chain

A chain of scopes that runs from bottom to top (inside to out) to find variables along the scope chain

Rules for finding variables:

In the current scope of the execution context to find the corresponding attribute, if there is a direct return, otherwise into the upper level of the execution context to find the corresponding attribute, if there is no, up to the global scope chain, if not found, throw an exception.

\