29
rated 0 times
[
34]
[
5]
/ answers: 1 / hits: 15803
/ 13 Years ago, fri, july 1, 2011, 12:00:00
It seems that the following technique for checking the existence of an object member produces an error because the 'bar' parent object hasn't been declared before the check, which means I either have to declare it before the check or use two 'typeof' expressions, either of which would be excess code:
var foo = {},
newVal = (typeof foo.bar.myVal !== 'undefined' ? foo.bar.myVal : null );
Error: foo.bar is undefined
So, how do you check if a member within an undeclared object exists without producing an error?
I love javascript, but sometimes...
More From » object