Skip to content

Instantly share code, notes, and snippets.

@kavanagh
Created June 2, 2012 11:00
Show Gist options
  • Save kavanagh/2857796 to your computer and use it in GitHub Desktop.
Save kavanagh/2857796 to your computer and use it in GitHub Desktop.
Simple namespace function
(function(global){
global.namespace = function namespace(name, context) {
var names = name.split('.'), name;
context = context || global;
while (name = names.shift()) {
context = context[name] = context[name] || new function(){};
}
return context;
};
}(this));
@kavanagh
Copy link
Author

kavanagh commented Jun 2, 2012

// using the context parameter
namespace('my.package', window);

my.package.Thing = function Thing(){};

namespace('more.namespacing', my.package);

my.package.more.namespacing.Thing = function Thing(){};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment