Skip to content

Instantly share code, notes, and snippets.

@thewillk
Created May 23, 2012 15:17
Show Gist options
  • Save thewillk/2775844 to your computer and use it in GitHub Desktop.
Save thewillk/2775844 to your computer and use it in GitHub Desktop.
Basic Namespacing/Sandboxing System
(function(){
var n$,
decorate,
expand,
libraries = [],
set_libraries;
n$ = function(cb){
if(cb.apply) {
cb.apply(n$,libraries);
}
return n$;
}
set_libraries = function() {
if( arguments.length > 0 ) {
libraries = arguments;
}
return libraries;
};
decorate = function(name,value) {
var i;
if( arguments.length > 1 ) {
this[name] = value;
}
else {
for(i in name) {
this[i] = name[i];
}
}
};
expand = function(name){
var i, that = this;
if( name && !(name in that) ) {
this[name] = function(cb){
if(cb.apply) {
cb.apply(that[name],libraries);
}
return that[name];
};
this[name]['expand'] = function(){return expand.apply(that[name],arguments);};
this[name]['decorate'] = function(){return decorate.apply(that[name],arguments);};
}
return this[name];
};
n$['expand'] = function(){return expand.apply(n$,arguments);};
n$['decorate'] = function(){return decorate.apply(n$,arguments);};
n$['libs'] = set_libraries;
this['n$'] = n$;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment