Skip to content

Instantly share code, notes, and snippets.

@colingourlay
Created April 14, 2012 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save colingourlay/2383021 to your computer and use it in GitHub Desktop.
Save colingourlay/2383021 to your computer and use it in GitHub Desktop.
JavaScript namespacing (depends on jQuery)
(function (global, $) {
global.namespace = function (nsName, obj) {
var level, names, name;
level = global;
names = nsName.split('.');
while(name = names.shift()) {
level[name] = level[name] || {};
level = level[name];
}
$.extend(level, obj);
}
})(this, jQuery);
// Usage:
namespace('one.two.three', {
four: (function () {})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment