The execution of JavaScript code

The running process of JavaScript code is essentially divided into two stages — compilation stage and execution stage.

Lexical analysis(Esprima.org/demo/parse….That is, to analyze some variables of code or function, declare, and deal with duplicate named variables. Converting from char stream to Token stream

The AST (Abstract Syntax Tree) interpreter then interprets execution according to the Syntax Tree

Precompile, when the JavaScript engine parses the script, it processes all declared variables and functions at precompile time! And is pre-declared variables, and then pre-defined functions.

Take an example: function runtime

function test(a,b){ console.log(a); var c = 123; console.log(c); function a(){}; // This is the function declaration console.log(b); var b = function c(){}; // This is the function expression console.log(b); } the test (1, 3);Copy the code
  • 1. Create a activation Object,AO{} 2: Store all the parameters and variable declarations in the function in the AO object. Value is undipay

  • 2. Store all the parameters and variable declarations in the function in the AO object, with value being undipay

    AO{ 
        a:undefined, 
        b:undefined, 
        c:undefined 
     }​
    Copy the code
  • 3. Unify parameters and arguments

    AO{ 
        a:1, 
        b:3, 
        c:undefined 
    }​
Copy the code
  • 4. Function declarations are advanced
    AO{ 
        a:function a(){}, 
        b:3, 
        c:undefined 
      }​
Copy the code
  • 5. Execution result

Function a () {},

In 123,

3,

function c(){}

run