Skip to content

Instantly share code, notes, and snippets.

@thebird
Forked from nzakas/namespace.js
Created December 6, 2011 19:23
Show Gist options
  • Save thebird/1439539 to your computer and use it in GitHub Desktop.
Save thebird/1439539 to your computer and use it in GitHub Desktop.
A single global with a namespace method
//BSD Licensed
var YourGlobal = {
namespace: function(ns){
var parts = ns.split("."),
object = this,
i, len;
for (i=0, len=parts.length; i < len; i++) {
if (!object[parts[i]]) {
object[parts[i]] = {};
}
object = object[parts[i]];
}
return object;
}
};
//usage
YourGlobal.namespace("foo.bar");
YourGlobal.foo.bar.message = "Hello world!";
YourGlobal.namespace("foo").baz = true;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment