Skip to content

Instantly share code, notes, and snippets.

@milosdakic
Created December 12, 2011 03:54
Show Gist options
  • Save milosdakic/1464718 to your computer and use it in GitHub Desktop.
Save milosdakic/1464718 to your computer and use it in GitHub Desktop.
JavaScript namespacing with Underscore
function namespace(string, obj) {
var current = window,
names = string.split('.'),
name;
while(name = names.shift()) {
current[name] = current[name] || {};
current = current[name];
}
_.extend(current,obj);
}
// usage
namespace('Superawesome', {
Function: function() {
// awesome output
}
});
namespace('Superawesome.Second', {
Wow: function() {
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment