Skip to content

Instantly share code, notes, and snippets.

@ThisIsMissEm
Created November 9, 2009 10:15
Show Gist options
  • Save ThisIsMissEm/229854 to your computer and use it in GitHub Desktop.
Save ThisIsMissEm/229854 to your computer and use it in GitHub Desktop.
jet.namespace = function(/*String*/ namespace, /*Object?*/ properties){
// summary:
// Creates & Resolves an objects structure based on the given Namespace string.
// namespace:
// A string representing an object tree, each level separated by a period.
// example:
// | jet.namespace("a.b.c");
// | #=> a = {}; a.b={}; a.b.c={};
// example:
// | jet.namespace("a.b").c = function(){};
// example:
// | jet.namespace("a.b.c", function(){});
var current = jet.global;
for(var node, parts = namespace.split('.'); parts.length && (node = parts.shift());){
if(properties && parts.length == 0){
if(Object.prototype.toString.call(properties) === '[object Function]'){
current = (current[node] = properties);
} else {
current = (current[node] = jet.mixin(current[node], properties) || {});
}
} else {
current = (current[node] = (current[node] === undefined) ? {} : current[node]);
}
}
return current;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment