Created
June 3, 2011 07:21
-
-
Save jiggliemon/1006005 to your computer and use it in GitHub Desktop.
Simple namespacing 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
if(!Object.namespace) | |
Object.namespace = function( str ){ | |
if(!str) return; | |
var i = 0 | |
,base = window | |
,shards,length | |
if(str.indexOf("::") !== -1){ | |
shards = str.split("::"); | |
base = window[shards[0]] = window[shards[0]] || {}; | |
str = shards[1]; | |
} | |
str = str.split('.'); | |
length = str.length; | |
for(;i<length;i++){ | |
base = base[str[i]] = base[str[i]] || {}; | |
} | |
if(arguments.length > 1 && Object.extend){ | |
var args = Array.prototype.slice.call(arguments,1); | |
base = Object.extend.apply(base, args); | |
} | |
return base; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment