If I want to test of object x
has a defined property y
regardless of x.y
's value, is there a better way that the rather clunky:
if ( typeof(x.y) != 'undefined' ) ...
?
If I want to test of object
? |
|||
|
Object has property:If you are testing for properties that are on the object itself (not a part of its prototype chain) you can use
Object or its prototype has a property:You can use the
|
|||||||||||||||||||||
|
If you want to know if the object physically contains the property @gnarf's answer using If you're want to know if the property exists anywhere, either on the object itself or up in the prototype chain, you can use the
Eg.:
|
|||||||||||||
|
You can trim that up a bit like this:
|
|||||||||||||
|
One feature of my original code
that might be useful in some situations is that it is safe to use whether So perhaps all three methods have a place in one's bag of tricks. |
|||||||||
|
Underscore.js
:) |
|||||||||
|