JS variable promotion

any great god knows why the last console.log reported an error
I tried debugger
and found that the if statement cannot be entered. An is equal to

of 1.
Mar.20,2021

aPP is 0 , then an is 1


aPP increases itself, first participate in the operation, and then add 1.


you change var axi0 to var axi1 try.


because the an of if (aPP) {} is still 0, execute the following console.log (a ())


directly.

this topic typically examines the promotion of js variables. Note that the promotion of js variables is only a common saying. Strictly speaking, this type of problem still examines the underlying execution process of js, that is, how js code is interpreted and compiled (some people may say: fuck,js compiles? You've got to be kidding me.), implement a complete set of processes, and now let's explain

in terms of variable promotion.

it is specifically stated here that the test of the landlord should be carried out in chrome. You should try it in ie. The first console.log should print a function. Ie and standard browsers have different pre-parsing methods for variables and functions under such conditions as if.

you can check the information: https://codeshelper.com/a/11.

"

there are two pits:
one, there is such an order in the process of variable promotion, function parameter > function > variable. In addition, the assignment operation overrides the declaration process.
for this question is equivalent to:

function a(){}
var a;
a = 0;// function a,
....

II. Differences in compilation process of js engine
Code:

console.log(a);
var a = 0;
console.log(a);
if (aPP) {
    function a() {console.log('function a')};
}
console.log(a());
  • chrome v8 optimizes when compiling the code that does not execute (dead code) (here, the condition is false). My understanding is that the function a () {} section is removed. So there is no ascension when function an is executed.
undefined
0
// 
  • IE Chakra ((which I tested in IE11) will compile function a () {}, which is the same as the function and variable promotion we expected.
function a() {console.log('function a')};
0
// 
Menu