如何在不出现引用错误的情况下将不存在的 JavaScript 对象与 undefined 进行比较?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11380283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I compare a non-existing JavaScript object to undefined without getting a Reference Error?
提问by Fawkes5
I want to boolean to come out of this expression
我想布尔值从这个表达式中出来
(task === undefined);
where task
is arbitrary and doesn't appear in the code at all.
wheretask
是任意的,根本没有出现在代码中。
However, when I run this in rhino, I get a reference Error. I WANT TRUE
但是,当我在 rhino 中运行它时,出现参考错误。我想要真实
Why don't I get true?
为什么我不明白?
I want to check if a particular variable has been defined. How do I do it then if this doesn't work?
我想检查是否已定义特定变量。如果这不起作用,我该怎么做?
回答by Ned Batchelder
Use this:
用这个:
(typeof task === "undefined")
When you use (task === undefined)
, Javascript needs to find the value of task
to see if it is the same as undefined
, but it can't look up the name because it doesn't exist, giving you the reference error. typeof
is special in that it can safely return the type of a name that doesn't exist.
当你使用 时(task === undefined)
,Javascript需要查找 的值,task
看是否与 相同undefined
,但是由于名称不存在而查不到,给你引用错误。 typeof
特殊之处在于它可以安全地返回不存在的名称的类型。
回答by Spoike
Addendumto the accepted answer to understand why it doesn't work with some examples you can try yourself in a javascript console.
已接受答案的附录以了解为什么它不适用于某些示例,您可以在 javascript 控制台中尝试自己。
Comparing directly with undefined type only works if the variable exist. Below is the output you'll get from the Google Chrome browser:
直接与未定义类型进行比较仅在变量存在时才有效。以下是您将从 Google Chrome 浏览器获得的输出:
> task === undefined
ReferenceError: task is not defined
However if the variable existsit will work:
但是,如果变量存在,它将起作用:
// continued from above
> var task
undefined
> task === undefined
true
This is the reason why you should use typeof
solution instead because it will work in allcases without throwing errors (and breaking the execution of javascript code).
这就是为什么你应该使用typeof
解决方案的原因,因为它可以在所有情况下工作而不会抛出错误(并中断 javascript 代码的执行)。
// continued from above
> typeof notavariable === 'undefined'
true
> typeof task === 'undefined'
true
Note that you don't need the typeof
check in some cases, such as the properties in a object literal:
请注意,typeof
在某些情况下您不需要检查,例如对象字面量中的属性:
// continued from above
> var obj = {}
undefined
> obj.test === undefined
true
> obj.test = 1
1
> obj.test === undefined
false
This is because properties in an object behave more like values in an associative array:
这是因为对象中的属性表现得更像关联数组中的值:
// continued from above
> obj["test"]
1
> obj["test"] === undefined
false
However you can't always be sure this is a case in a function where you have no control over the argument input:
但是,您不能总是确定这是在您无法控制参数输入的函数中的情况:
// continued from above
> function TestFunc(arg1) { console.log(arg1) }
undefined
> TestFunc(notavariable)
ReferenceError: notavariable is not defined
> TestFunc(task)
undefined
undefined
> TestFunc(obj["lol"])
undefined
undefined
Hope this exercise helps you out to understand the why's of this comparison.
希望这个练习能帮助你理解这种比较的原因。