Analysis of JavaScript Precompilation and Implied Global Variables

  • 2021-08-12 01:48:29
  • OfStack

1. Implying global variables

Undeclared variables are called implied global variables.


var a = 1; // Variables declared outside a function are called global variables 
b = 2; //  Undeclared variables outside or inside a function are called implied global variables 
function fn() {
 var c = 3; // Variables declared in a function body are called local variables 
 d = 4; //  Implied global variable 
}
fn(); //  If the function is not executed, the function is not precompiled, d  It will not be promoted to a global variable 
console.log(c); // error: c is not defined
console.log(d); // 4

2. JavaScript execution process

1. Syntax analysis, if there are low-level syntax errors, it will not be compiled and executed;
2. Precompile, including variable declaration and function declaration in advance;
3. Explain execution, explain 1 line, execute 1 line.

3. Precompile

Precompilation can be divided into global precompilation and function precompilation.

Precompilation can be divided into global precompilation and function precompilation.

1. After the js script is loaded, it will check whether there are low-level errors throughout;
2. After syntax detection, global precompilation is carried out;
3. After global precompilation, interpret 1 line and execute 1 line;
4. When executing to the line of function call, the function will be precompiled before executing.

Global precompilation:

1. Create the global object GO (window object);
2. Declare variables ahead of time, putting the declarations of all variables at the front as properties of GO objects,
And assign undefined, if there are variables with the same name, only one is declared;
3. Advance the function declaration and put the function declaration at the front as an attribute of the GO object.
If the function name is the same as the variable name, the variable name will be overwritten by the function name, and the value is the function body.
This is why the function definition can be put before or after the function call.

Function precompilation:

1. At 1 moment before the execution of the function, the precompilation of the function comes on stage;
2. Create an AO object (Active Object) first;
3. Declare formal parameters and variables in advance and assign undefined as the attribute of AO;
4. Assign arguments to formal parameters;
5. The function declaration is advanced, and the value is the function body, which is the attribute of AO.

Chestnuts:


var a = 1;
function b(c){
 console.log(c);
 var c = 2;
 console.log(c);
 function c() {}
 var d = 3;
 function e() {}
}
b(4);

First analyze the global precompilation,

Create an GO object, GO = {}; Variable declaration advanced

//  Pseudocode 
GO = { 
 a = undefined
}
Function declaration advance

//  Pseudocode 
GO = { 
 a = undefined
 b = f b(c) { console.log(c); ... }
}

Re-analyze function precompilation,

Create an AO object, AO = {}; Formal parameters and variables are declared in advance;


//  Pseudocode 
AO = { 
c = undefined //  Same as the variable name, only declaring 1 A 
d = undefined
}
Assign arguments to formal parameters;

//  Pseudocode 
AO = { 
c = 4 // b(4) The argument passed in is 4
d = undefined
}

Function declaration advance


//  Pseudocode 
AO = { 
c = f c() {} //  Function name overrides variable name 
d = undefined
e = f e() {}
}

So the first time you print the variable c, it's the output function body, not argument 4.

The above is the detailed analysis of JavaScript precompilation and implied global variables, more information about JavaScript precompilation and implied global variables please pay attention to other related articles on this site!


Related articles: