Created
April 6, 2016 14:20
-
-
Save yesoreyeram/bb37d793cbbe2eeccb3661740acfb33d to your computer and use it in GitHub Desktop.
Creating Namespaces 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() { | |
"use strict"; | |
this.myapp = function(namespaceString, parentObj, delimiter) { | |
var d, f, e; | |
parentObj = parentObj || myapp || this; | |
namespaceString = namespaceString.split(delimiter || "."); | |
d = 0; | |
for (f = namespaceString.length; d < f; d++) | |
{ | |
delimiter = namespaceString[d], | |
parentObj = parentObj[delimiter] = parentObj[delimiter] || {}, | |
"object" === typeof parentObj && (e = parentObj); | |
} | |
"function" === typeof parentObj && e && (parentObj = parentObj.bind(e)); | |
return parentObj; | |
}; | |
}.call(this)); | |
// // How To use | |
// myapp("world.asia.india"); | |
// myapp("africa", myapp.world); | |
// myapp("europe-england", myapp.world,"-"); | |
// myapp("world-africa-kenya", undefined ,"-"); | |
// console.log(myapp.world); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment