only initWrapper() once. make wrapper constructor accessable as _._wrapper.

Previously, there was no way to get at the OO wrapper object.
So if a user added a custom method on to _, or defined a custom
alias, it would not be reflected in the wrapper.  Now it is at
least possible, though there is no API to do it.  I alluded to
making one in my commit that added _.alias() (which I recently reverted,
as it added weight to the stripped version), but honestly I think
the wrapping behavior should go in to a seperate library, not because
it is bad, but because it is an awesome pattern that I want to use
on objects other than underscore.
This commit is contained in:
Mike Frawley
2010-02-23 18:20:42 -06:00
parent c9b41a4996
commit b8fd129130

View File

@@ -645,11 +645,17 @@
_.buildWrapper = function () { throw "Call _.initWrapper() to enable OO wrapping" }
_.initWrapper = function () {
if (_._wrapper) return; // Already initialized
// If Underscore is called as a function, it returns a wrapped object that
// can be used OO-style. This wrapper holds altered versions of all the
// underscore functions. Wrapped objects may be chained.
var wrapper = function(obj) { this._wrapped = obj; };
// Make wrapper object public so user code can extend it.
// Otherwise, there is no way to modify its prototype
_._wrapper = wrapper;
// Overwrite method called from _()
_.buildWrapper = function (obj) { return new wrapper(obj) };
@@ -698,5 +704,6 @@
}
// For backwards compatability, init the OO wrapper
// the advanced minifying rake task will strip this out
_.initWrapper();
})();