Skip to content

Instantly share code, notes, and snippets.

@BrianRosamilia
Last active August 29, 2015 14:25
Show Gist options
  • Save BrianRosamilia/5441481a1a8758907e80 to your computer and use it in GitHub Desktop.
Save BrianRosamilia/5441481a1a8758907e80 to your computer and use it in GitHub Desktop.
JS Boilerplate for accessing long property chains
Object.prototype.getChainedProperty = function(str) {
var t = str.split('.');
var value = this;
for(x = 0; x < t.length; x++){
if(value[t[x]] === undefined){
return undefined;
}
value = value[t[x]];
}
return value;
}
var a = { b : { c : { d : 1 } } };
console.log(a.getChainedProperty('b.c.d'));
var k = {}
console.log(k.getChainedProperty('d'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment