Created
December 6, 2011 19:14
-
-
Save nzakas/1439504 to your computer and use it in GitHub Desktop.
A single global with a namespace method
This file contains 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
//BSD Licensed | |
var YourGlobal = { | |
namespace: function(ns){ | |
var parts = ns.split("."), | |
object = this, | |
i, len; | |
for (i=0, len=parts.length; i < len; i++) { | |
if (!object[parts[i]]) { | |
object[parts[i]] = {}; | |
} | |
object = object[parts[i]]; | |
} | |
return object; | |
} | |
}; | |
//usage | |
YourGlobal.namespace("foo.bar"); | |
YourGlobal.foo.bar.message = "Hello world!"; | |
YourGlobal.namespace("foo").baz = true; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@nzakas I thought you're intention was to show new function that may help with client-side modules organization. I just wanted to shed a light on new techniques that developed recently and will be part of standard in a future (Harmony modules), that's it. If I misunderstood your point, sorry about that.