Skip to content

Instantly share code, notes, and snippets.

@jonathantneal
Created February 22, 2012 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathantneal/1881687 to your computer and use it in GitHub Desktop.
Save jonathantneal/1881687 to your computer and use it in GitHub Desktop.
Better Getters
function property(obj, prop, val) {
for (var o = obj, i = 0, a = prop.split('.'), l = a.length; i < l; ++i) {
if (!(a[i] in o)) return;
if (val && i == l - 1) {
o[a[i]] = val;
}
o = o[a[i]];
}
return val != null ? obj : o;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment