promote

 

Regarding promotion, remember a few key points, and all subsequent judgments can be based on this key point:

1. When executing the code, the first step is always static compilation, the compiler will parse all declared variables, and then bind them to the corresponding scope, and in the second step, the engine will start to perform assignments and other operations;

2. Function declarations always take precedence over variable declarations. For the same function declaration, the latter overrides the former;

Let's see an example:

a = 2;
var a;
console.log(a);

The first step will be word segmentation analysis, a=2, in fact, a lexical analysis will be performed first, which is not in the current scope (note that var a has not been run at this time), so an a variable is automatically defined in the global scope .

The second step is var a, because the current scope (under the global) has defined the variable a, so this step is ignored. (Assignment is skipped here, because assignment is the execution action of the engine, and only after static editing, the code execution begins a few microseconds before execution.)

After the above two steps are completed, the compilation work is completed, and the next step is the engine execution link.

The first step is to assign a value to a, LHS search, and directly assign the value to 2.

The second step is to perform an RHS search on the console, then confirm that there is a log method under the console object, and then perform an RHS search on a. In the current scope, a is found to be 2, so the execution prints out 2.

 

Look at the example again:

console.log(a);
var a = 2;

The first step, the compilation process, only var a is a variable declaration, so variable a is defined.

At this point, the compilation is complete, and the engine comes on stage

The first step, execute console.log(a), as above, RHS twice, find that a is undefined, because the assignment of a is behind it, so print undefined

 

foo();
function(){}

Why the function declaration can be promoted, because the function declaration does not perform the assignment operation, it is the compilation process is completed, so when the function is executed, there is already a definition.

 

foo();
var foo = function(){}

This will report an error, what is wrong? TypeError, because var foo will be executed first in the compilation phase, and foo() will perform RHS lookup and find foo, but foo is not a function and cannot be executed, so a type error is reported.

 

foo();

var foo;

function foo(){console.log(1)}

foo = function() {console.log(2)}

The order of execution of the above code should be:

function foo() {console.log(1)}

var foo;

foo();

foo = function(){console.log(2)}

Here, foo will not be overwritten because it will be ignored. For the reason, please refer to the first step of compilation / lexical analysis.

 

 

Combined with the compilation principle, these codes are really easy to understand, and there are no problems.

 

 

 

 

end

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325526960&siteId=291194637
Recommended