简体   繁体   English

对Java语言吊装感到困惑

[英]Confused about Javascript Hoisting

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

In the above code of javascript, during the hoisting phase, will the function b be hoisted? 在上述javascript代码中,在提升阶段,函数b会被提升? Or just a will be hoisted because only function a is sitting lexically in the global context. 或只是将a挂起,因为在全局上下文中,只有函数a在词法上坐着。

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. 在提升阶段调用该函数( a )时, b将托管在它出现的范围的顶部(由函数a的主体定义)。

b will not be exported to the global scope. b将不会导出到全局范围。

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

Declarations are hoisted to the top of their containing scope, which for function b is function a . 声明被提升到其包含范围的顶部,对于函数b是函数a

Function b will be hoisted to the top of function a , but that is where it already is. 函数b将被提升到函数a的顶部,但是那已经存在了。

And, function a (based on your code) will be hoisted to the top of the Global scope. 并且,函数a (基于您的代码)将被提升到Global范围的顶部。

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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