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?
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?
The error has nothing to do with typeof
, it's because you're attempting to access a property of an undefined variable: 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
. 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)
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...
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)
{
//...
}
}
}