Instead of this:
if( person !== undefined && person.details !== undefined && person.details.city !== undefined )
Using this method you can use this:
if( person.testPath('details.city') )
Object.prototype.testPath = function(path){ | |
var pieces = path.split('.'), | |
currPath = this, | |
passed = true; | |
pieces.forEach(function(val){ | |
if(currPath[val] === undefined){ | |
passed = false; | |
} else { | |
currPath = currPath[val]; | |
} | |
}); | |
return passed; | |
}; |