Skip to content

Instantly share code, notes, and snippets.

@jackwanders
Last active August 29, 2015 14:05
Show Gist options
  • Save jackwanders/929656ed18c3fb0bf001 to your computer and use it in GitHub Desktop.
Save jackwanders/929656ed18c3fb0bf001 to your computer and use it in GitHub Desktop.
A proof of concept method for creating multiple levels of hierarchy within an object in one shot, regardless of whether they already exist
/**
* PropAddr
*/
var PropAddr = function(obj) {
var props = [],
i;
if(arguments.length === 2) {
props = arguments[1].split('.');
} else {
props = Array.prototype.slice.call(arguments, 1);
}
if(props.length < 1) { return; }
if(typeof obj[props[0]] === 'undefined') {
obj[props[0]] = {};
}
PropAddr.apply(this, [obj[props[0]]].concat(props.slice(1)));
};
// Examples
// PropAddr(window, 'propAddrObj.level1.level2.level3');
// PropAddr(window, 'propAddrObj2', 'level4', 'level5', 'level6');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment