Skip to content

Instantly share code, notes, and snippets.

@JiriChara
Created November 15, 2012 21:08
Show Gist options
  • Save JiriChara/4081280 to your computer and use it in GitHub Desktop.
Save JiriChara/4081280 to your computer and use it in GitHub Desktop.
Javascript Namespaces
var MYAPP = MYAPP || {};
MYAPP.namespace = function (ns_string) {
var parts = ns_string.split('.'),
parent = MYAPP,
i;
// strip redundant leading global
if (parts[0] === "MYAPP") {
parts = parts.slice(1);
}
for (i = 0; i < parts.length; i += 1) {
// create a 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