简体   繁体   English

typeof(未定义的变量)导致错误…?

[英]typeof (undefined variable) causing error…?

alert(typeof QuizParser.Parser.otherdata['response_set']['answers']["slide_" + _index]['trt']);​

Why? 为什么? Shouldn't this simply alert the string undefined ? 这不应该只是警告字符串undefined吗? If this is wrong, how should I check to see if that variable is defined or not? 如果这是错误的,我应该如何检查该变量是否已定义?

该错误与typeof无关,这是因为您试图访问未定义变量的属性: QuizParser.Parser

When you use typeof , it will return "undefined" for undefined variables, but in your case you are actually doing other things before calling typeof . 当您使用typeof ,它将为未定义的变量返回"undefined" ,但是在您这种情况下,您实际上是在调用typeof之前做其他事情。 QuizParser.Parser.otherdata must be defined in order for typeof to not cause an error. 必须定义QuizParser.Parser.otherdata ,以便typeof不会导致错误。 For example, if x is not defined, typeof(x) is okay but typeof(x.something) will cause an error since you are trying to access x which is not defined (in order to access something) 例如,如果未定义x ,则typeof(x)可以,但是typeof(x.something)会导致错误,因为您正尝试访问未定义的x (以便访问某些内容)

Well, it's not that easy, if you choose the direct way: you'd have to write something like... 好吧,如果您选择直接方式,那就不是那么容易了:您必须编写类似...

if (typeof QuizParser !== 'undefined' // to check whether the variable defined or not
    && QuizParser.Whatever            // if variable IS defined, we can check its property directly
    && QuizParser.Whatever.Property... 
)

Take note that we cannot skip the 'middle' chains: if property doesn't exist, it will be evaluated to undefined, and the next step will throw a TypeError: Cannot read property ... of undefined . 请注意,我们不能跳过“中间”链:如果属性不存在,它将被评估为undefined,下一步将抛出TypeError: Cannot read property ... of undefined

But there's another way (and it's quite common in many cross-browser libraries) - use exceptions to catch 'missing links' instead: 但是还有另一种方式(这在许多跨浏览器的库中很常见)-使用异常来捕获“缺少的链接”:

try {
  var flag = QuizParser.Whatever.Property.You.Like['equal']['to']['something'] !== undefined;
} catch(e) {}
if (flag) { ... }​ // processing goes here

With this you can more-o-less emulate isset behaviour from PHP: your flag variable will be set to true if and only if that endpoint property of the target object is set (= not undefined ). 这样,您就可以从PHP模仿isset行为:当且仅当设置了目标对象的端点属性(= not undefined )时,您的flag变量才会设置为true And with exceptions mechanism you guarantee that no error will be thrown at your client (stopping the JS parsing altogether) otherwise... 并使用例外机制来确保不会向客户端抛出任何错误(完全停止JS解析),否则...

It's not the typeof that's the issue. 这不是typeof这是个问题。 It's the fact that inside the typeof , before the "typeof" part is actually executed, you're trying to access members of a null object. 这是事实,在typeof内部,在实际执行“ typeof”部分之前,您正在尝试访问null对象的成员。 For example, consider this scenario (and note that it isn't javascript, but it's the idea I'm trying to get across, not the language syntax) 例如,请考虑这种情况(请注意,它不是javascript,但这是我试图理解的想法,而不是语言语法)

public class Test
{
    public string teststring;
}

and then you do something like this: 然后您执行以下操作:

Test nullTest; // null

if(typeof(test.teststring) != null)

the second the parser sees the dot after test, a nullreference error is thrown, since you're essentially trying to call null.teststring. 解析器在测试后第二秒看到点,则抛出nullreference错误,因为您实际上是在尝试调用null.teststring。 Instead, you have to do something like: 相反,您必须执行以下操作:

if(object != null && object.property != null && object.property.propertyOfTheProperty != null) 
//...

so that the execution of the if statement would be broken before anything dangerous could ever happen. 这样就可以在任何危险发生之前中断if语句的执行。 If knowing about object 's or property 's nullness is important to you, you can also do this: 如果了解object的空值或property的空值对您很重要,则也可以执行以下操作:

if(object != null)
{
    if(object.property != null)
    {
        if(object.property.propertyOfTheProperty != null)
        {
           //...
        }
    }
}

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

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