测试 JavaScript 中是否有未定义的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7041123/
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
Test if something is not undefined in JavaScript
提问by Raimonds
I'm checking if(response[0].title !== undefined)
, but I get the error:
我正在检查if(response[0].title !== undefined)
,但出现错误:
Uncaught TypeError: Cannot read property 'title' of undefined.
未捕获的类型错误:无法读取未定义的属性“标题”。
回答by amosrivera
response[0]
is not defined, check if it is defined and then check for its property title.
response[0]
未定义,请检查是否已定义,然后检查其属性标题。
if(typeof response[0] !== 'undefined' && typeof response[0].title !== 'undefined'){
//Do something
}
回答by Rion Williams
Just check if response[0]
is undefined:
只需检查是否response[0]
未定义:
if(response[0] !== undefined) { ... }
If you still need to explicitly check the title, do so after the initial check:
如果您仍然需要明确检查标题,请在初始检查后进行:
if(response[0] !== undefined && response[0].title !== undefined){ ... }
回答by Karl Henselin
I had trouble with all of the other code examples above. In Chrome, this was the condition that worked for me:
我在上面的所有其他代码示例中遇到了麻烦。在 Chrome 中,这是对我有用的条件:
typeof possiblyUndefinedVariable !== "undefined"
I will have to test that in other browsers and see how things go I suppose.
我将不得不在其他浏览器中进行测试,看看我想的情况如何。
回答by André Luiz
Actually you must surround it with an Try/Catch block so your code won't stop from working. Like this:
实际上,您必须用 Try/Catch 块包围它,这样您的代码就不会停止工作。像这样:
try{
if(typeof response[0].title !== 'undefined') {
doSomething();
}
}catch(e){
console.log('responde[0].title is undefined');
}
回答by Jeffrey Mark Baldridge
typeof:
类型:
var foo;
if (typeof foo == "undefined"){
//do stuff
}
回答by Tanmay Chandra
Check if condition == null
;
It will resolve the problem
检查是否condition == null
; 它将解决问题
回答by DavidGouge
It'll be because response[0]
itself is undefined.
这将是因为response[0]
它本身是未定义的。
回答by AngeLOL
I know i went here 7 months late, but I found this questions and it looks interesting. I tried this on my browser console.
我知道我来这里晚了 7 个月,但我发现了这些问题,看起来很有趣。我在浏览器控制台上试过这个。
try{x,true}catch(e){false}
If variable x is undefined, error is catched and it will be false, if not, it will return true. So you can use eval function to set the value to a variable
如果变量 x 未定义,则捕获错误并返回 false,否则返回 true。所以你可以使用 eval 函数将值设置为变量
var isxdefined = eval('try{x,true}catch(e){false}')
回答by BadHorsie
In some of these answers there is a fundamental misunderstanding about how to use typeof
.
在其中一些答案中,对如何使用typeof
.
Incorrect
不正确
if (typeof myVar === undefined) {
Correct
正确的
if (typeof myVar === 'undefined') {
The reason is that typeof
returns a string. Therefore, you should be checking that it returned the string "undefined" rather than undefined
(not enclosed in quotation marks), which is itself one of JavaScript's primitive types. The typeof
operator will never return a value of type undefined
.
原因是typeof
返回一个字符串。因此,您应该检查它是否返回了字符串 "undefined" 而不是undefined
(未括在引号中),它本身就是JavaScript 的原始类型之一。该typeof
运营商将不会返回类型的值undefined
。
Addendum
附录
Your code might technically work if you use the incorrect comparison, but probably not for the reason you think. There is no preexisting undefined
variable in JavaScript - it's not some sort of magic keyword you can compare things to. You can actually create a variable called undefined
and give it any value you like.
如果您使用不正确的比较,您的代码在技术上可能会起作用,但可能不是因为您认为的原因。undefined
JavaScript 中没有预先存在的变量——它不是某种可以与事物进行比较的魔法关键字。您实际上可以创建一个名为的变量undefined
并为其赋予您喜欢的任何值。
let undefined = 42;
And here is an example of how you can use this to prove the first method is incorrect:
这是一个如何使用它来证明第一种方法不正确的示例:
回答by Exelian
Check if you're response[0]
actually exists, the error seems to suggest it doesn't.
检查您是否response[0]
确实存在,错误似乎表明它不存在。