Created
February 1, 2012 15:52
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]]; | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: {
}
}
}