Skip to content

Instantly share code, notes, and snippets.

@geastwood
Last active December 24, 2015 21:09
Show Gist options
  • Save geastwood/6863740 to your computer and use it in GitHub Desktop.
Save geastwood/6863740 to your computer and use it in GitHub Desktop.
simple namespace function
// simple namespace function
// use: var newNs = namespace('this.is.a.new.namespace');
var namespace = function(namespace, context) {
if (!namespace) { throw 'namespace string is required' };
var ns = namespace.split('.');
var current = context || this;
for (var k in ns) {
if (!current[ns[k]]) {
current[ns[k]] = {};
}
current = current[ns[k]];
}
return current;
};
console.log(namespace('fei.liu.fl'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment