Skip to content

Instantly share code, notes, and snippets.

@benjaminramey
Created February 1, 2012 15:52
Show Gist options
  • Select an option

  • Save benjaminramey/1717672 to your computer and use it in GitHub Desktop.

Select an option

Save benjaminramey/1717672 to your computer and use it in GitHub Desktop.
Simulates namespaces in Javascript by taking and dividing up a dot-separated string and creating empty objects for each namespace depth.
registerNamespace: function(nspace)
{
var collection = nspace.split('.');
var registeredNamespace = collection[0];
var root = window
for (var i = 0; i < collection.length; i++)
{
if (!root[collection[i]])
{
root[collection[i]] = {};
}
root = root[collection[i]];
}
}
@benjaminramey

Copy link
Copy Markdown
Author

Used for easy creation of a namespace "path" so that you don't have to create each object along the way manually. You pass in a string like "Bob.Joe.Sam" and get the object structure
Bob = {
Joe: {
Sam: {
}
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment