It's not nice in JavaScript.
You could add them to one big condition...
if (obj.prop && obj.prop.someOtherProp) { }
...or write a helper function where you pass an object and a string...
var isPropSet = function(object, propPath) {
return !! propPath.split('.')
.reduce(function(object, prop) { return object[prop] || {}; }, object);
};
isPropSet(obj, 'prop.someOtherProp);
...or you could use CoffeeScript and its ?
operator...
obj.prop?.someOtherProp
You could also wrap the lookup in a try/catch
, but I wouldn't recommend it.