Skip to content

Instantly share code, notes, and snippets.

@tisho
Forked from RStankov/gist:77625
Created March 11, 2009 19:21
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 tisho/77662 to your computer and use it in GitHub Desktop.
Save tisho/77662 to your computer and use it in GitHub Desktop.
// Simplified one-liner version with a bit of added functionality
String.prototype.namespace = function(objects, scope, separator){
return Object.extend(this.split(separator || '.').inject((scope || window), function(parent, child){
return (parent[child] = parent[child] || {});
}), objects || {});
}
// Sample usage
'com.pixeldepo.some.package'.namespace();
'com.pixeldepo.some.other.package'.namespace();
'foo.bar'.namespace({}, 'com.pixeldepo'); // creates com.pixeldepo.foo.bar
'foo::bar'.namespace({}, 'com.pixeldepo', '::'); // creates com.pixeldepo.foo.bar
'bar.baz'.namespace({ foo: 'bar' }); // creates bar.baz and adds { foo: 'bar' } to it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment