JavaScript“无法读取未定义的属性”栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8004617/
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
JavaScript "cannot read property "bar" of undefined
提问by Connor
I've got a function that takes 3 parameters. The problem I have is one of the parameters is a property of a sometimes undefined value of an Object (i.e. it takes in thing.foo.bar, and sometimes thing.foois undefined, so it can't access bar).
我有一个接受 3 个参数的函数。我遇到的问题是其中一个参数是对象的有时未定义值的属性(即它接受thing.foo.bar,有时thing.foo未定义,因此它无法访问bar)。
What's a way around this? Within the function's declaration, I have a conditional checking:
if (!parameterName)
, but the browser (Chrome) is still throwing an error that it can't read the barproperty of undefined.
有什么办法解决这个问题?在函数的声明中,我有一个条件检查:
if (!parameterName)
,但浏览器(Chrome)仍然抛出一个错误,它无法读取未定义的bar属性。
回答by nnnnnn
If an object's property may refer to some other object then you can test thatfor undefined before trying to use its properties:
如果一个对象的属性可能引用某个其他对象,那么您可以在尝试使用其属性之前测试它是否为 undefined:
if (thing && thing.foo)
alert(thing.foo.bar);
I could update my answer to better reflect your situation if you show some actual code, but possibly something like this:
如果您显示一些实际代码,我可以更新我的答案以更好地反映您的情况,但可能是这样的:
function someFunc(parameterName) {
if (parameterName && parameterName.foo)
alert(parameterName.foo.bar);
}
回答by Marc B
Compound checking:
复合检查:
if (thing.foo && thing.foo.bar) {
... thing.foor.bar exists;
}
回答by jfriend00
You can safeguard yourself either of these two ways:
您可以通过以下两种方式保护自己:
function myFunc(thing) {
if (thing && thing.foo && thing.foo.bar) {
// safe to use thing.foo.bar here
}
}
function myFunc(thing) {
try {
var x = thing.foo.bar;
// do something with x
} catch(e) {
// do whatever you want when thing.foo.bar didn't work
}
}
In the first example, you explicitly check all the possible elements of the variable you're referencing to make sure it's safe before using it so you don't get any unplanned reference exceptions.
在第一个示例中,您在使用之前明确检查您引用的变量的所有可能元素以确保它是安全的,这样您就不会遇到任何计划外的引用异常。
In the second example, you just put an exception handler around it. You just access thing.foo.bar
assuming it exists. If it does exist, then the code runs normally. If it doesn't exist, then it will throw an exception which you will catch and ignore. The end result is the same. If thing.foo.bar
exists, your code using it executes. If it doesn't exist that code does not execute. In all cases, the function runs normally.
在第二个示例中,您只需在其周围放置一个异常处理程序。您只需thing.foo.bar
假设它存在即可访问。如果确实存在,则代码正常运行。如果它不存在,那么它将抛出一个异常,您将捕获并忽略该异常。最终结果是一样的。如果thing.foo.bar
存在,您使用它的代码就会执行。如果它不存在,则代码不会执行。在所有情况下,该函数都正常运行。
The if
statement is faster to execute. The exception can be simpler to code and use in complex cases where there may be many possible things to protect against and your code is structured so that throwing an exception and handling it is a clean way to skip execution when some piece of data does not exist. Exceptions are a bit slower when the exception is thrown.
该if
语句执行速度更快。在复杂的情况下,异常可以更简单地编码和使用,在这种情况下,可能有许多可能的事情需要保护,并且您的代码结构化,以便在某些数据不存在时抛出异常并处理它是一种跳过执行的干净方法. 抛出异常时异常会慢一些。
回答by Petar Ivanov
Just check for it before you pass to your function. So you would pass:
只需在传递给函数之前检查它。所以你会通过:
thing.foo ? thing.foo.bar : undefined