这个Javascript代码片段如何打印'false'?(How does this Javascript code snippet print 'false'

编程入门 行业动态 更新时间:2024-10-22 18:26:37
这个Javascript代码片段如何打印'false'?(How does this Javascript code snippet print 'false'?)

看看下面的代码,我不明白(typeof a!=='undefined')是如何工作的。 我知道变量“a”不在IIFE中,所以它在它之外是未定义的。 但是我不明白代码是如何打印'false'的

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

如果我删除typeof并执行

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

我得到一个错误,说未捕获的ReferenceError:a未定义。 那么当typeof存在时它如何正常运行 - 并且当删除typeof时它会出错? 那是我的困惑

Looking at the code below, I don't understand how (typeof a !== 'undefined') works. I understand that variable "a" is not inside the IIFE, so it is undefined outside of it. But I don't understand how the code prints out '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

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

I get an error saying Uncaught ReferenceError: a is not defined. So how is it that it runs fine when typeof is present- and it errors when typeof is removed? That's my confusion

最满意答案

首先,帖子是否存在“typeof”互换 - 它应该插入第一个例子中并在第二个例子中删除。 但不管怎么说。

那么当typeof存在时它如何正常运行 - 并且当删除typeof时它会出错?

答案在于typeof运算符的性质和设计。

无论是否处于严格模式,读取未声明变量的值以获取其值都会生成“参考错误:a未定义”, 除非使用typeof运算符

typeof运算符旨在允许测试未声明的变量,并返回undefined的原始值(如果尚未声明)。 在没有错误的情况下,确实没有其他方法可以执行此测试。

如果没有清楚地理解测试的原因,测试结果仍然不明确:值为undefined的声明变量的类型也返回“undefined”。

因此,在第一个(?)示例中,如果测试类型为typeof a !== 'undefined , a在IIFE之外是未定义/未声明的,则返回“undefined”类型,与不等式测试中的自身相比,返回false。

在没有'typeof`的情况下进行测试时,您会收到参考错误。

Firstly the post has the presence or absence of "typeof" interchanged - it should be inserted in the first example and removed in the second. But anyway.

So how is it that it runs fine when typeof is present- and it errors when typeof is removed?

The answer is in the nature and design of the typeof operator.

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.

The typeof operator was designed to allow testing for an undeclared variable and returns the primitive value undefined if it has not been declared. 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".

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.

When tested without 'typeof` you get the reference error.

更多推荐

本文发布于:2023-04-28 01:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329680.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:片段   代码   Javascript   false   print

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!