current position:Home>Unveil the mystery of ascension

Unveil the mystery of ascension

2021-08-27 07:25:59 It's not easy to keep your dreams

I believe that many people have such questions , What is promotion ? How to understand this concept ? Remember the first class in College c Language class , What the teacher said on the podium , It's still fresh in my memory ,“ The code is executed from top to bottom ”, But in JavaScript in , Is that really the case ? My answer is not entirely correct .

hello = 2;
var hello;
 Copy code 

Maybe after reading this code, many small partners will think it is undefined.

Maybe you're wrong ...

Let's go over the details , stay var hello The statement in hello = 2 Behind , Many people think that variables have been reassigned , Therefore, it is given a default value undefined. however , The result is 2.


var world = 3;
 Copy code 

Maybe many people think the result is 3, I'm sorry you're wrong again ! The result is undefined

Think differently

All declarations, including variables and functions, take precedence before any code is executed .

Before that , Maybe many friends still have doubts about the concept of declaration , For example, call the function first and then define , It should be noted that , Each scope is promoted .


function() {
    console.log(a);  //undefined
    var a = 2;

 Copy code 
foo();   //  No ReferenceError   It is  TypeError

var foo = function bar(){ // do someThing }
 Copy code 
Function declaration

Function declaration is promoted , But function expressions are not promoted , Why? ? because foo hold bar Memory address of , and foo Promoted and assigned to the scope ( overall situation ) therefore foo() Will not lead to ReferenceError, however foo Not assigned at this time , What needs to be added here is , Suppose it's a function declaration instead of a function expression , Then it will be assigned ,foo() Due to undefined Value to make a function call and cause an illegal operation, it will throw TypeError.

foo() //TypeError

bar() //ReferenceError 

var foo = function bar(){ //do someThing }

 Copy code 
Why function first

Function declaration and variable declaration will be promoted , But we need to understand one detail , The function will be promoted first , The second is the variable

foo(); //1
var foo;

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

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

 Copy code 

Why would it be 1 instead of 2 Well ?

We can think of it this way :

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

foo(); //1

foo =  function (){ console.log(2);}
 Copy code 
Repeat the definition

Take a look at the following code , We can come to a conclusion : Despite repeated definitions foo, However, the functions that appear later will override the previous ones .

foo(); //3 

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

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

function foo(){ console.log(3);}
 Copy code 

Take a look at the following code

foo(); TypeError: foo is not function 

var z = true;

if(z) {
  function foo() { console.log("z"); }
} else {
  function foo() { console.log("x"); }

 Copy code 
Come to the conclusion

We draw the following conclusion from the above code : var a = 2; We usually think of it as a statement , But in fact, the engine doesn't think so , It will be var a = 2; As two separate statements , The first is the task in the compilation phase , The second is the task of the implementation phase .

The second is whatever is used in the declaration anywhere , Will reduce the code itself to be processed first before execution , And this process can be understood as all declarations ( Variables and functions ), Will be moved to the top of their respective scopes , This process is called ascension .

Finally, the statement itself will be promoted , But assignment operations, including the assignment of function expressions, do not promote .

copyright notice
author[It's not easy to keep your dreams],Please bring the original link to reprint, thank you.

Random recommended