简体   繁体   English

了解JavaScript吊装与真假

[英]Understanding JavaScript hoisting and truthy & falsy

I've been reading about JavaScript hoisting sometime back.我一直在阅读有关 JavaScript 吊装的信息。

JavaScript Scoping and Hoisting by Ben Cherry JavaScript Ben Cherry 的范围和提升
Two words about “hoisting” by Dmitry Soshnikov德米特里·索什尼科夫(Dmitry Soshnikov)关于“吊装”的两个词

and, some more about JavaScript type-coercion, truth & false test: Truth, Equality and JavaScript and some other resource还有更多关于 JavaScript 类型强制、真假测试: Truth, Equality 和 JavaScript和其他一些资源

And while practicing some, and found I m missing some important concept about the hoisting and a variable' truthy & falsy.在练习一些时,发现我错过了一些关于提升和变量“真假”的重要概念。

1: 'if' truth test with duplicate variable declaration 1:带有重复变量声明的“if”真值测试

var foo = 1; 
function bar() { 
    if (!foo) { 
    alert('inside if');
        var foo = 10; 
    } 

} 
bar();

o/p: inside if o/p: inside if

Doubt: 'foo' value being '1', if(!foo) should evaluates to false and that block should not be executed (quoting from above resources: hoisting affects only the var & function declaration, but not the execution).怀疑: 'foo' 值为 '1', if(!foo)的计算结果应为false ,并且不应执行该块(引用上述资源:提升仅影响var & function声明,但不影响执行)。 But why is that alert is shown.但是为什么会显示该警报。 This is not the case if I directly use false (shown in the below no-tricks code: snippet #3)如果我直接使用false则不是这种情况(如下面的无技巧代码所示:片段 #3)

2: 'if' truth test without duplicate variable declaration 2:没有重复变量声明的“if”真值测试

var foo = 1; 
function bar() { 
    if (!foo) { 
        alert('inside if');
    } 

} 
bar();

o/p: no output; o/p:没有 output; means control not entered 'if' block表示控件未进入“if”块
This is what one could expect这是人们可以期待的

3: 'if' using 'false' with duplicate variable declaration 3: 'if' 使用 'false' 重复变量声明

var foo = 1; 
function bar() { 
    if (false) { 
        alert('inside if');
        var foo = 10; 
    } 
} 
bar();

o/p: no output; o/p:没有 output; means control not entered 'if' block表示控件未进入“if”块
This is what one could expect这是人们可以期待的

Someone please clarify.有人请澄清。 Thanks谢谢

For your example number 1, the alert is shown because you're using var inside the function and the var declaration is hoisted to the top of the function, so it is equivalent to:对于您的示例编号 1,显示警报是因为您在 function 中使用var并且var声明被提升到 function 的顶部,因此它相当于:


var foo = 1; 
function bar() {
    var foo;
    if (!foo) {
        alert('inside if');
        foo = 10; 
    } 

} 
bar();

One might conclude that these sorts of issues offer compelling reason to declare all variables explicitly at the top of the function.人们可能会得出结论,这些问题提供了令人信服的理由,可以在 function 的顶部显式声明所有变量。

Only variable declaration is eagerly evaluated.只有变量声明被热切评估。 The variable assignment in your first case (in the if block) occurs only upon entering the if block.第一种情况下的变量赋值(在if块中)仅在进入if块时发生。

The variable you only declare , but not assign any value to, has the value of undefined (which coerces to false ).您仅声明但未分配任何值的变量具有undefined值(强制为false )。

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

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