Skip to content

Instantly share code, notes, and snippets.

@jiggliemon
Created July 31, 2012 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jiggliemon/3215076 to your computer and use it in GitHub Desktop.
Save jiggliemon/3215076 to your computer and use it in GitHub Desktop.
/*global define:false, require:false */
define(['./extend'], function (extend) {
'use strict';
var _spaces = {};
function namespace ( str ) {
var args = Array.prototype.slice.call(arguments,1)
,i = 0
,base = window
,shards,length;
if (!str) {
return;
}
if (str.indexOf("::") >= 0) {
shards = str.split("::");
base = window[shards[0]] = window[shards[0]] || {};
str = shards[1];
}
str = str.split('.');
length = str.length;
for (i=0;i<length;i++) {
base = base[str[i]] = base[str[i]] || {};
}
if (arguments.length > 1 && Object.extend) {
base = extend.call(base, args);
}
return base;
};
// todo: Utilize the define property of namespace to cache/reference
// namespaces. This is for future use
// method: define || alias?
//
// namespace.define = function(str,obj){
// spaces[str] = obj;
// };
return namespace;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment