Skip to content

Instantly share code, notes, and snippets.

@fabiocicerchia
Created February 25, 2012 08:24
Show Gist options
  • Save fabiocicerchia/1907333 to your computer and use it in GitHub Desktop.
Save fabiocicerchia/1907333 to your computer and use it in GitHub Desktop.
Javascript - Register a namespace
function registerNS(ns) {
var nsParts = ns.split('.');
var root = window;
for(var i = 0; i < nsParts.length; i++) {
if(typeof root[nsParts[i]] == 'undefined') {
root[nsParts[i]] = new Object();
}
root = root[nsParts[i]];
}
}
// EXAMPLE
registerNS('namespace.sub_namespace1.sub_namespace2');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment