Skip to content

Instantly share code, notes, and snippets.

@npow
Created June 2, 2012 20:18
Show Gist options
  • Save npow/2859804 to your computer and use it in GitHub Desktop.
Save npow/2859804 to your computer and use it in GitHub Desktop.
Namespace
function namespace(str) {
var L = str.split('.');
function ns(last, L) {
if (!L.length) return;
ns(last[L[0]] = last[L[0]] || {}, L.splice(1))
}
ns(window[L[0]] = window[L[0]] || {}, L.splice(1))
}
namespace("x.y.z");
namespace("x.b.c");
x.y.z.foo = 5;
console.log(x.y.z.foo);​​​​​​​​​​​​​​​​​​
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment