Skip to content

Instantly share code, notes, and snippets.

@Couto
Created May 10, 2012 14:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Couto/2653308 to your computer and use it in GitHub Desktop.
Save Couto/2653308 to your computer and use it in GitHub Desktop.
Small & Recursive Namespace function
function namespace (ns) {
var parts = ns.split('.'),
root = window || this;
return (function walk (obj) {
if (!root[obj]) { root[obj] = {}; }
root = root[obj];
if (parts.length) { walk(parts.shift()); }
return root;
}(parts.shift()));
}
// Example
namespace('MyApp.Controllers.Menu').Sidebar = (function () {
// Code
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment