Namespaced made easy with Prototype
Date : 2008 02 04 Category : Tech & Developmentkangax keeps up his "Prototype by example"-ness by showing a use of Enumerable#inject.
He shows us namespacing made easy:
PLAIN TEXT JAVASCRIPT:String.prototype.namespace = function(separator) { this.split(separator || '.').inject(window, function(parent, child) { return parent[child] = { }; }); }
And then you can use it via:
PLAIN TEXT JAVASCRIPT:// Default separator is '.' 'foo.bar.baz.quux'.namespace(); foo.bar.baz.quux = 'Nothing special...'; // => "Nothing special..." // Or using a custom one 'MY_AWESOME_APPLICATION::util::DOM::dimensions'.namespace('::'); 'dimensions' in MY_AWESOME_APPLICATION.util.DOM; // => true