JavaScript hoisting function vs function variable

前端 未结 4 898
礼貌的吻别
礼貌的吻别 2021-01-22 17:34

Here is my javascript code :

    console.log(a);
    c();
    b();            
    var a = \'Hello World\';
    var b = function(){
        console.log(\"B is ca         


        
4条回答
  •  佛祖请我去吃肉
    2021-01-22 18:09

    A Function Declaration will be hoisted along with its body.

    A Function Expression not, only the var statement will be hoisted.


    This is how your code "looks" like to the interpreter after compiletime - before runtime:

     var c = function c(){
          console.log("C is called");
     }
    
     var a = undefined
     var b = undefined
    
     console.log(a); // undefined at this point
     c(); // can be called since it has been hoisted completely
     b(); // undefined at this point (error)
    
     a = 'Hello World';
     b = function(){
         console.log("B is called");
     }
    

    KISSJavaScript

提交回复
热议问题