Created
July 25, 2012 19:03
-
-
Save keithelliott/3177922 to your computer and use it in GitHub Desktop.
Creating javascript namespaces
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
// Chatham Namespace for exposing javascript functionality | |
// We can expose all of our functionality via this one global var | |
var CHATHAM = CHATHAM || {}; | |
CHATHAM.namespace = function (namespace_name_string) { | |
var parts = namespace_name_string.split('.'), | |
parent = CHATHAM, | |
i, cnt; | |
// remove the first part of the string which will be 'CHATHAM' | |
if (parts[0] === 'CHATHAM') { | |
parts = parts.slice(1); | |
} | |
cnt = parts.length; | |
for (i = 0; i < cnt; i += 1) { | |
// create and add the property if it doesn't exist | |
if (typeof parent[parts[i]] === 'undefined') { | |
parent[parts[i]] = {}; | |
} | |
parent = parent[parts[i]]; | |
} | |
return parent; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment