简体   繁体   English

我应该使用(typeof(val)==='undefined')还是(val === undefined)?

[英]Should I use (typeof(val) === 'undefined') or (val === undefined)?

This is similar to a number of other questions on SO, but not exactly the same as any I can find. 这类似于关于SO的其他一些问题,但与我能找到的任何问题都不完全相同。

Which is the best approach for checking for an undefined value in Javascript, and why? 哪个是在Javascript中检查未定义值的最佳方法,为什么?

First example: 第一个例子:

var a;
if (typeof(a) === 'undefined'){...}

Second example: 第二个例子:

var a;
if (a === undefined){...}

So, the first example is comparing the name of the type to a string, and the second is comparing the variable to the undefined object, using the equality operator, which checks that the types are the same as well as the values. 因此,第一个示例是将类型的名称与字符串进行比较,第二个示例是使用等于运算符将变量与未定义的对象进行比较,该运算符检查类型和值是否相同。

Which is better? 哪个更好? Or are they both as good as one another? 或者他们俩都和对方一样好?

Note that I'm not asking about any difference between undefined and null, or truthy or falsey, just which of these two methods is correct and/or better. 请注意,我不是在询问undefined和null之间或truthy或falsey之间的任何区别,只是这两种方法中的哪一种是正确的和/或更好的。

If a variable doesn't exist, then you'll get a reference error when you try to use it — even if you are comparing it to undefined . 如果变量不存在,那么当您尝试使用它时,您将收到引用错误 - 即使您将其与undefined进行比较。 So always use typeof . 所以总是使用typeof

> foo === undefined
ReferenceError: foo is not defined
    at repl:1:2
    at REPLServer.eval (repl.js:80:21)
    at Interface.<anonymous> (repl.js:182:12)
    at Interface.emit (events.js:67:17)
    at Interface._onLine (readline.js:162:10)
    at Interface._line (readline.js:426:8)
    at Interface._ttyWrite (readline.js:603:14)
    at ReadStream.<anonymous> (readline.js:82:12)
    at ReadStream.emit (events.js:88:20)
    at ReadStream._emitKey (tty.js:320:10)
> typeof foo === "undefined"
true

It is also possible for (bad) code to overwrite undefined , which would cause an undefined value to not be equal to undefined . (坏)代码也可能覆盖undefined ,这会导致未定义的值不等于undefined

The undefined can be assigned a value, and the type check won't work. 可以为undefined分配一个值,并且类型检查不起作用。 Unless the scope of the code is protected, eg 除非代码的范围受到保护,例如

(function(undefined){
    var a;
    if (a === undefined) {
})();
// note called without parameter, so undefined is actually an undefined value

this way to check is not safe, and first one is prefered 这种方式检查是不安全的,首先是首选

Edit: It seems that ECMA 5 dissalows assigning value to the undefined, but still this depends on the browser implementation. 编辑:似乎ECMA 5不能为undefined分配值,但这仍然取决于浏览器的实现。

The two methods are correct, but the typeof one is immune to changes to the value of undefined . 这两种方法是正确的,但是typeof一个是免疫改变的价值undefined If you need stricter checking, use typeof() . 如果需要更严格的检查,请使用typeof()

In the ECMA 3 standard, you can modify the value of undefined as such: 在ECMA 3标准中,您可以修改undefined的值,如下所示:

undefined = "not undefined";

And this can lead to ugliness when comparing to undefined later on. 与以后的undefined相比,这可能导致丑陋。 In ECMA 5, this is disallowed. 在ECMA 5中,这是不允许的。 This means most modern browsers will not let you set the value of undefined , and you should be safe using === undefined . 这意味着大多数现代浏览器都不会让你设置undefined的值,你应该安全地使用=== undefined

Also, if you're not even sure whether the variable you are checking has been defined, you should use typeof , otherwise you will get a reference error. 此外,如果您甚至不确定是否已定义要检查的变量,则应使用typeof ,否则将出现引用错误。

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

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