简体   繁体   中英

Confused about Javascript Hoisting

function a(){
  function b(){
  }
}

In the above code of javascript, during the hoisting phase, will the function b be hoisted? Or just a will be hoisted because only function a is sitting lexically in the global context.

b will be hosted to the top of the scope it appears in (the scope defined by the body of function a ) during the hoisting phase when that function ( a ) is invoked.

b will not be exported to the global scope.

函数a将被提升到全局范围的顶部(假定此范围位于全局范围内),函数b将被提升到由函数a创建的范围的顶部。

Declarations are hoisted to the top of their containing scope, which for function b is function a .

Function b will be hoisted to the top of function a , but that is where it already is.

And, function a (based on your code) will be hoisted to the top of the Global scope.

In hoisting process all the declarations will move up below the parent function declaration.

Ex: function fun(){
    a = 10;
    var c = b();
    function b(){}
}

will become like

function fun(){
 var a;
 var c;
 function b(){};
 a = 10;
 c = b();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM