Skip to content

Instantly share code, notes, and snippets.

@jfsiii
Created February 13, 2010 18:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfsiii/303614 to your computer and use it in GitHub Desktop.
Save jfsiii/303614 to your computer and use it in GitHub Desktop.
/*
* ns('foo.bar');
* => window.foo = {bar: {} };
* ns('some.arbitrary.depth.string');
* => window.some = {arbitrary: {depth: {string: {} } };
* ns('some.other.ns');
* => window.some is already created, so window.some.other = {ns: {}};
*/
function ns( str, root )
{
var parts = str.split('.'),
part, ndx = -1,
len = parts.length;
root = root || this;
while (++ndx < len) {
part = parts[ndx];
if (!(part in root)) root[part] = {};
root = root[part];
}
return root;
}​
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment