简体   繁体   English

此Javascript代码段如何显示“ false”?

[英]How does this Javascript code snippet print 'false'?

Looking at the code below, I don't understand how (typeof a !== 'undefined') works. 看下面的代码,我不明白(typeof!=='undefined')的工作方式。 I understand that variable "a" is not inside the IIFE, so it is undefined outside of it. 我知道变量“ a”不在IIFE内,因此在其外部未定义。 But I don't understand how the code prints out 'false' 但是我不明白代码是如何输出“ false”的

(function(){
   var a = 3;   
})();

console.log("a defined? " + (typeof a !== 'undefined'));
//prints false- but how and why?

If I remove the typeof and just execute 如果我删除typeof并执行

console.log("a defined? " + (typeof a !== 'undefined')); 
Uncaught ReferenceError: a is not defined

I get an error saying Uncaught ReferenceError: a is not defined. 我收到一条错误消息,提示未定义ReferenceError:未定义。 So how is it that it runs fine when typeof is present- and it errors when typeof is removed? 那么,当存在typeof时,它如何正常运行?而当移除typeof时,它又出错了? That's my confusion 那是我的困惑

As you say, a doesn't exist outside the IFFE. 如您所说,在IFFE之外不存在a It is therefore undefined in the different scope. 因此,在不同范围内未定义。

//Enter the following in your console.
typeof a   // --> "undefined"

(function(){
   var a = 3;   
})();

typeof a   // --> "undefined" (Still)

The issue may come when you're checking the type, and perhaps misunderstanding the Boolean evaluation (it's kind of a double negative). 当您检查类型时,可能会出现问题,并且可能会误解布尔值(有点双重否定)。 typeof a === 'undefined' is true , therefore, typeof a !== 'undefined' is false . typeof a === 'undefined'true ,因此, typeof a !== 'undefined'false

Firstly the post has the presence or absence of "typeof" interchanged - it should be inserted in the first example and removed in the second. 首先,帖子中是否存在“ typeof”互换的信息-应该在第一个示例中插入,然后在第二个示例中将其删除。 But anyway. 但无论如何。

So how is it that it runs fine when typeof is present- and it errors when typeof is removed? 那么,当存在typeof时,它如何正常运行?而当移除typeof时,它又出错了?

The answer is in the nature and design of the typeof operator. 答案在于typeof运算符的性质和设计。

Whether in strict mode or not, reading the value of an undeclared variable to get its value generates a "Reference Error: a is not defined" except when using the typeof operator . 不管是否处于严格模式下, 除非使用typeof运算符,否则读取未声明变量的值以获取其值都会生成“参考错误:未定义”。

The typeof operator was designed to allow testing for an undeclared variable and returns the primitive value undefined if it has not been declared. typeof运算符旨在允许测试未声明的变量,如果未声明,则返回undefined的原始值。 There really is no other means of performing this test without thowing an error. 确实没有其他方法可以在不出错的情况下执行此测试。

The test result is still ambiguous if the reason for the test is not clearly understood: the type of a declared variable with value undefined also returns "undefined". 如果不清楚测试的原因,则测试结果仍然是不确定的:声明值为undefined变量的类型也会返回“ undefined”。

So in the first(?) example if you test typeof a !== 'undefined , a is undefined/undeclared outside the IIFE, typeof a returns "undefined" which compared to itself in an inequality test returns false. 所以在第一个(?)例如,如果你测试typeof a !== 'undefineda未定义/未申报的外IIFE, typeof a返回‘不确定’这比起自己在不等式测试返回false。

When tested without 'typeof` you get the reference error. 在没有'typeof'的情况下进行测试时,您会得到参考错误。

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

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