We all know JS execution follows two rules
1. function declaration Overall promotion
2. Variable declaration Promotion
In fact, the most important thing is pre-compilation, pre-compilation often occurs before the function execution, understanding of pre-compilation, we understand function execution is very critical.
Pre-compilation Prelude
1.imply global implies that any variable, if the variable is assigned without declaration, is owned by the global attribute.
2. The global variables of all declarations are all properties of window.
Let's look at a few examples here.
Example 1 function fn (a) {
Console.log (a);
var a = 123;
Console.log (a);
function A () {};
Console.log (a);
var B = function () {};//This is a function expression, not a function declaration (Var b belongs to a variable declaration)
Console.log (b);
Function d () {}
}
fn (1);
1. Create an AO object
ao{
}
2. Find function parameters and function inside variables declaration, parameter name and variable name as properties of AO object, value is undefined
ao{
a:undefined;
b:undefined;
}
3. The argument is unified, the argument value is assigned to the parameter (that is, the value of the argument is assigned to the parameter, and the assignment within the function is not performed)
ao{
a:1;
b:undefined;
}
4. Find function declaration, function name as an attribute of AO object, value is function body
ao{
A:function A () {};
b:undefined;
D:function d () {};
}
After all execution
ao{
a:123;
B:function () {};
D:function d () {};
}
function Execution Result
function fn (a) {
Console.log (a); function A () {}
var a = 123;
Console.log (a); 123
function A () {};
Console.log (a); 123
var B = function () {};//This is a function expression, not a function declaration (Var b belongs to a variable declaration)
Console.log (b); function () {};
Function d () {}
}
fn (1);
Example 2 function test (A, b) {
Console.log (a);
c = 0;
var C;
A = 3;
b = 2;
Console.log (b);
Function B () {};
Function d () {};
Console.log (b);
}
Test (1);
1. Create an AO object
ao{
}
2. Find formal parameters and variable declarations within functions, and declare parameters and variable declarations as values for AO objects with a value of undefined
ao{
a:undefined;
b:undefined;
c:undefined;
}
3. Formal parameter arguments are unified, and the values of the arguments are assigned to the parameters
ao{
a:1;
b:undefined;
c:undefined;
}
4. Find the function declaration, the function name is the property of the Ao object, the value is the function body
ao{
a:1;
B:function B () {};
c:undefined;
D:function d () {}
}
After all execution
ao{
a:1->3;
B:2;
c:0;
D:function d () {};
}
function Execution Result
function Test (A, b) {
Console.log (a);//1
c = 0;
var C;
A = 3;
b = 2;
Console.log (b);//2
Function B () {};
Function d () {};
Console.log (b);//2
}
Test (1);
Example 3 function test (A, b) {
Console.log (a);
Console.log (b);
var B = 234;
Console.log (b);
a=123;
Console.log (a);
function A () {};
var A;
b = 234;
var B = function () {}
Console.log (a);
Console.log (b);
}
Test (1);
1. Create an AO object
ao{
}
2. Find the function parameters and variables within the function declaration, parameter names and function names as properties of the Ao object, the value is undefined
ao{
a:undefined;
b:undefined;
}
3. Unify the parameters with the arguments and assign the parameter values to the parameters
ao{
a:1;
b:undefined;
}
4. Find function declaration, function name as an attribute of AO object, value is function body
ao{
A:function A () {};
b:undefined;
}
Execution results
function Test (A, b) {
Console.log (a); function A () {}
Console.log (b);//undefined
var B = 234;
Console.log (b);//234
a=123;
Console.log (a);//123
function A () {};
var A;
b = 234;
var B = function () {}
Console.log (a);//123
Console.log (b);//function () {};
}
Test (1);
Precompilation occurs not only in functions, but also in the global, when it turns out to form a go (Global object//window object)
Example 4
Console.log (test);
function test (test) {
Console.log (test);
var test = 234;
Console.log (test);
function Test () {
}
}
Test (1);
var test= 123;
1. First create a Go
go{
}
2. Find the global variable declaration
go{
test:undefined;
}
4. Find function declarations
go{
Test:function Test (test) {
Console.log (test);
var test = 234;
Console.log (test);
function Test () {
}
}
}
1. First create an AO
ao{
}
2. Find the function parameters and variables within the function declaration, parameter names and function names as properties of the Ao object, the value is undefined
ao{
test:undefined;
}
3. Unify the parameters with the arguments and assign the parameter values to the parameters
ao{
Test:1;
}
4. Find function declaration, function name as an attribute of AO object, value is function body
ao{
Test:function Test () {
};
}
Example 5
global = 100;
function fn () {
Console.log (global);
global = 200;
Console.log (global);
var global = 300;
}
FN ();
var global;
1.go{
global:100;
}
2.ao{
global:undefined;
}
Execution results
global = 100;
function fn () {
Console.log (global); Undefined
global = 200;
Console.log (global); 200
var global = 300;
}
FN ();
var global;
Example 6
function Test () {
Console.log (b);
if (a) {
var b = 180;
}
c = 234;
Console.log (c)
}
var A;
Test ();
A = 10;
Console.log (c);
1.go{
a:undefined;
Test:function Test () {
Console.log (b);
if (a) {
var b = 180;
}
c = 234;
Console.log (c)
}
}
2.ao{
b:undefined;
}
Execution results
function Test () {
Console.log (b); Undefined
if (a) {
var b = 180;
}
c = 234;
Console.log (c); 234
}
var A;
Test ();
A = 10;
Important things, say how many times are not counted as Doha, and finally summed up:
Pre-compilation (before function execution) ※
1. Create an AO object (Active Object--execution context)
2. Find function parameters and function inside variables declaration, parameter name and variable name as properties of AO object, value is undefined
3. The argument is uniform, and the parameter value is assigned to the formal parameter.
4. Find function declarations, function names as properties of AO objects, values as function references
Pre-compilation (script code block before execution)
1. Find global variable declaration (including implicit global variable declaration, omit var declaration), Variable masterpiece Global object property, value undefined
3. Find function declarations, function names as properties of global objects, values as function references
JS Pre-compilation