From b8fd129130f0956b43d5336452b1a5f709685b55 Mon Sep 17 00:00:00 2001 From: Mike Frawley Date: Tue, 23 Feb 2010 18:20:42 -0600 Subject: [PATCH] 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. --- underscore.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/underscore.js b/underscore.js index 6260b57b8..d8c795e7e 100644 --- a/underscore.js +++ b/underscore.js @@ -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(); })();