Last active
December 24, 2015 21:09
-
-
Save geastwood/6863740 to your computer and use it in GitHub Desktop.
simple namespace function
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
// simple namespace function | |
// use: var newNs = namespace('this.is.a.new.namespace'); | |
var namespace = function(namespace, context) { | |
if (!namespace) { throw 'namespace string is required' }; | |
var ns = namespace.split('.'); | |
var current = context || this; | |
for (var k in ns) { | |
if (!current[ns[k]]) { | |
current[ns[k]] = {}; | |
} | |
current = current[ns[k]]; | |
} | |
return current; | |
}; | |
console.log(namespace('fei.liu.fl')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment