Skip to content

Instantly share code, notes, and snippets.

@craigrbruce
Created April 14, 2013 23:52
Show Gist options
  • Save craigrbruce/5384753 to your computer and use it in GitHub Desktop.
Save craigrbruce/5384753 to your computer and use it in GitHub Desktop.
Create Nested Namespace from String in JavaScript
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