-
-
Save tisho/77662 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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