Created
December 6, 2011 23:09
-
-
Save matthanger/1440502 to your computer and use it in GitHub Desktop.
Namespaces in JavaScript
This file contains 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
/** | |
* Registers the specified namespace, ensuring all parts of the namespace are created. If the namespace | |
* already exists, it will not be modified. | |
* | |
* Usage: | |
* namespace('foo.bar'); | |
* foo.bar.baz = 'qux'; | |
* | |
* @param {String} namespace The namespace to register. Namespaces are hierarchal, and are separated by dots. | |
*/ | |
window.namespace = function(namespace) { | |
var parts = namespace.split("."); | |
var root = window; | |
for(var i=0; i<parts.length; i++) { | |
root = root[parts[i]] = root[parts[i]] || {}; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment