Last active
August 29, 2015 14:25
-
-
Save BrianRosamilia/5441481a1a8758907e80 to your computer and use it in GitHub Desktop.
JS Boilerplate for accessing long property chains
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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