Skip to content

Instantly share code, notes, and snippets.

@shaunwallace
Last active August 29, 2015 14:09
Show Gist options
  • Save shaunwallace/f396c3b407361b395952 to your computer and use it in GitHub Desktop.
Save shaunwallace/f396c3b407361b395952 to your computer and use it in GitHub Desktop.
Simple Namespacing Method
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;
};
//Stefanov, Stoyan (2010-09-09). JavaScript Patterns (Kindle Locations 2886-2892). OReilly Media - A. Kindle Edition.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment