I wrote the method below, and was wondering whether a better way exists/best practice tips
_objectIsDefined = function (obj, path) {
if (typeof path !== 'undefined') {
// if we have been given a specific object path then recursively search it
var pathSplit = path.split("."),
scope = undefined;
scope = obj;
for (i = 0; i < pathSplit.length; i++) {
if (typeof scope[pathSplit[i]] === 'undefined') {
return false;
} else if (i == pathSplit.length - 1 && typeof scope[pathSplit[i]] !== 'undefined') {
return true;
}
scope = obj[pathSplit[i]];
}
return false;
} else if (typeof obj !== 'undefined') {
// if we've just been given an object and no path then return whether
// its defined or not...
return true;
} else {
return false;
}
}
// Usage
var isDefined = _objectIsDefined(myObject);
var isDefinedDeep = _objectIsDefined(myObject, "somenested.child.property.name");