Skip to content

Instantly share code, notes, and snippets.

@keithelliott
Created July 25, 2012 19:03
Show Gist options
  • Save keithelliott/3177922 to your computer and use it in GitHub Desktop.
Save keithelliott/3177922 to your computer and use it in GitHub Desktop.
Creating javascript namespaces
// 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