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

Why? Shouldn't this simply alert the string undefined? If this is wrong, how should I check to see if that variable is defined or not?

David G
  • 94,763
  • 41
  • 167
  • 253
yahfree
  • 93
  • 1
  • 9
  • 6
    You're not typeof-ing one variable, but properties of an undefined object. – Rob W Aug 03 '12 at 22:01
  • You are probably interested in http://stackoverflow.com/questions/2631001/javascript-test-for-existence-of-nested-object-key – Felix Kling Aug 03 '12 at 22:04

4 Answers4

2

The error has nothing to do with typeof, it's because you're attempting to access a property of an undefined variable: QuizParser.Parser

Madbreaks
  • 19,094
  • 7
  • 58
  • 72
  • Thank you! I thought an undefined field of an undefined object would still logically be undefined. – yahfree Aug 03 '12 at 22:11
  • 1
    @yahfree: That's the problem...there's no such thing as an "undefined object". `undefined` is not an object, and doesn't convert to one, so trying to treat it like one (in your case, taking a property of it) will get you a ReferenceError. – cHao Aug 03 '12 at 22:17
  • For clarity, an attempt is made to read the property called "Parser" of the undefined variable `QuizParser` (the value of `QuizParser.Parser.otherdata` is never read, because an error is thrown before that). – Rob W Aug 03 '12 at 22:20
  • @Rob: Long as we're clarifying, the error gets thrown before even attempting to read `QuizParser.Parser`, when you try to look up `QuizParser`. (Unlike object properties, variables pretty much have to exist -- a variable that doesn't exist throws a ReferenceError when you attempt to use it pretty much anywhere outside a `typeof`.) – cHao Aug 03 '12 at 22:23
  • @cHao Have to disagree - he's got a typeof in front of it. The undefined variable is allowed there. It's the dot notation attempting to access the property of `QuizParser` that's causing the error. – Madbreaks Aug 03 '12 at 22:33
  • @Madbreaks: Disagree all you want. And then fire up a V8 console and type these lines in order: `typeof x.y`, `x = undefined;`, `typeof x.y`. See if the error doesn't change from "ReferenceError: x is not defined" to "TypeError: Cannot read property 'y' of undefined". Proving that the first problem is that the variable doesn't exist yet. – cHao Aug 03 '12 at 22:47
  • @cHao It's much easier than that, try this at the console `typeof QuizParser`. LMK. – Madbreaks Aug 03 '12 at 22:52
  • @Madbreaks: `typeof QuizParser` works. `typeof QuizParser.**anything**` doesn't, because QuizParser doesn't exist. If you say `QuizParser = undefined`, you get an entirely different error. – cHao Aug 03 '12 at 22:54
  • That is *exactly* what I said in my original answer. Thanks! – Madbreaks Aug 03 '12 at 22:55
  • @Madbreaks: No, you didn't. You said "the dot notation attempting to access the property of `QuizParser`" is what's doing it. And the first error is not caused by trying to look up a property. It's caused by the fact that you're attempting to use `QuizParser` (which doesn't exist) outside the context of `typeof QuizParser`. If you set it to `undefined` and then tried again, you. would. get. a. whole. other. error. Because you would have gotten past the first one (the nonexistent variable). – cHao Aug 03 '12 at 23:10
0

When you use typeof, it will return "undefined" for undefined variables, but in your case you are actually doing other things before calling typeof. QuizParser.Parser.otherdata must be defined in order for typeof to not cause an error. 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)

cHao
  • 84,970
  • 20
  • 145
  • 172
Calvin Jia
  • 856
  • 5
  • 5
0

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.

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). And with exceptions mechanism you guarantee that no error will be thrown at your client (stopping the JS parsing altogether) otherwise...

raina77ow
  • 103,633
  • 15
  • 192
  • 229
-2

It's not the typeof that's the issue. 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. 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)

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. 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 knowing about object's or property's nullness is important to you, you can also do this:

if(object != null)
{
    if(object.property != null)
    {
        if(object.property.propertyOfTheProperty != null)
        {
           //...
        }
    }
}
Phillip Schmidt
  • 8,805
  • 3
  • 43
  • 67
  • @cHao nor is what I wrote, necessarily. But the ideas still apply, and it would have taken a lot longer to show the same concept in javascript – Phillip Schmidt Aug 03 '12 at 22:11
  • Thanks for the downvote, whoever. I wish I could downvote your thought process that caused you to believe that javascript has anything whatsoever to do with this question. – Phillip Schmidt Aug 03 '12 at 22:16
  • 2
    @PhillipSchmidt *Not my dv* But concepts from Java are not by definition applicable to JavaScript. Throwing in Java is only confusing. – Rob W Aug 03 '12 at 22:17
  • @RobW lol, evidently I have to break it down a little. The concept that the OP is coming across is that you can't access members of a null object. That concept applies to java (or C# which is technically what I was going for), in the **exact same way** that it does to javascript. I reasonably assumed based on the context of the question that the OP would understand it just as well in a syntax that, in my opinion, makes the issue at hand much easier to understand. – Phillip Schmidt Aug 03 '12 at 22:23
  • 1
    You might wanna break it down a little more. See, an undefined object in JS is not `null` -- in fact, if your value is `null`, it's defined! Starting to see why the language is important yet? (BTW, not my downvote either, but i considered it.) – cHao Aug 03 '12 at 22:32
  • @cHao Again, that is (almost) totally irrelevant. It's the *idea* that is important. – Phillip Schmidt Aug 03 '12 at 23:22
  • 1
    And the *idea* would be a lot less muddied if you'd presented it in terms of the language in the question. And no, `null` not being `undefined` is pretty freaking relevant, cause you make a point of comparing stuff to it and all that. `null` and `undefined` are different values, and if you compare.....eh. Screw it. -1 til you can bother explaining in terms of JS. – cHao Aug 03 '12 at 23:40
  • @cHao Jesus. the *idea* of null is still the same. I get what you're saying. You don't have to explain javascript 101 to me. I'm employed full time as a web developer. Null != undefined. Right. The point you're too dense to understand is that I could just as easily have written `if(property != "the amount I enjoy arguing with people on stackoverflow")`, and it would have had the exact same effect. Sure, in javascript, that isn't null, but the point is still clear. I can safely assume that the OP, being a sensical human being, wouldn't think that you can just take C# code and throw it into some – Phillip Schmidt Aug 03 '12 at 23:52
  • @chao javascript code, and everything works all nice and well. You, on the other hand, don't seem as up-to-par with that idea. But that's fine, keep your -1, and I'll keep my answer, thanks. – Phillip Schmidt Aug 03 '12 at 23:53
  • @cHao on nicer terms, I'd like to add the (fairly important) qualifier that `the amount I like to be proven wrong` (is even nuller than) `the amount I enjoy arguing with people on stackoverflow` – Phillip Schmidt Aug 04 '12 at 00:05
  • Oh, i get the idea. But we're talking about people who might not know this stuff. You can't safely assume that the guy asking a JS question knows how different C# is, or understands the difference between `null` and `undefined` and simply "doesn't exist". He's here *because* he's not a JS pro. – cHao Aug 04 '12 at 02:35