Created
April 14, 2013 23:52
-
-
Save craigrbruce/5384753 to your computer and use it in GitHub Desktop.
Create Nested Namespace from String in JavaScript
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
function createNamespace(fromString){ | |
var split = fromString.split('.'); | |
var parent = window; | |
var current = ''; | |
var length = split.length; | |
for(var i = 0; i < length; i++){ | |
current = split[i]; | |
parent[current] = parent[current] || {}; | |
parent = parent[current]; | |
} | |
return parent; | |
} | |
//usage: createNamespace("foo.bar.com"); ... returns com for adding items to. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment