Created
June 2, 2012 20:18
-
-
Save npow/2859804 to your computer and use it in GitHub Desktop.
Namespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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