Skip to content

Instantly share code, notes, and snippets.

@vaibhavguptaIITD
Created April 17, 2013 12:53
Show Gist options
  • Save vaibhavguptaIITD/5404009 to your computer and use it in GitHub Desktop.
Save vaibhavguptaIITD/5404009 to your computer and use it in GitHub Desktop.
javascript: initialize an object with nested properties
var app = {}
app.ensure_struct = function(atts,scope){
var scope = scope || this;
if (atts == "") {
return scope;
}
var parts = atts.split('.'),
first_attribute = parts.shift();
if (typeof (scope[first_attribute]) === 'undefined') {
scope[first_attribute] = {};
}
app.ensure_struct(parts.join('.'), scope[first_attribute]);
return scope;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment