mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-11 11:27:50 +00:00
make _.initWrapper() that builds the wrapper prototye
The intention is that underscore Consumers MUST call _.initWrapper() in their code to allow the OO style usage, or a runtime error is thrown. This would break backwards compatability, so for the moment I call initWrapper myself at the end of underscore. However, if I comment out my call to initWrapper and don't call it in user code, then the compiler can strip an empty underscore with no user code from 2137 -> 100 bytes. Making the user explicitly say they want OO wrapping is the only way to achieve this, otherwise the compiler will always keep the OO wrapper code around.
This commit is contained in:
@@ -43,7 +43,7 @@
|
|||||||
native_keys = Object['keys'];
|
native_keys = Object['keys'];
|
||||||
|
|
||||||
// Create a safe reference to the Underscore object for reference below.
|
// Create a safe reference to the Underscore object for reference below.
|
||||||
var _ = function(obj) { return new wrapper(obj); };
|
var _ = function(obj) { return _.buildWrapper(obj) };
|
||||||
|
|
||||||
// Export the Underscore object for CommonJS.
|
// Export the Underscore object for CommonJS.
|
||||||
if (typeof exports !== 'undefined') exports._ = _;
|
if (typeof exports !== 'undefined') exports._ = _;
|
||||||
@@ -68,8 +68,11 @@
|
|||||||
} else if (_.isNumber(obj.length)) {
|
} else if (_.isNumber(obj.length)) {
|
||||||
for (var i=0, l=obj.length; i<l; i++) iterator.call(context, obj[i], i, obj);
|
for (var i=0, l=obj.length; i<l; i++) iterator.call(context, obj[i], i, obj);
|
||||||
} else {
|
} else {
|
||||||
var keys = _.keys(obj), l = keys.length;
|
for (var key in obj)
|
||||||
for (var i=0; i<l; i++) iterator.call(context, obj[keys[i]], keys[i], obj);
|
if (hasOwnProperty.call(obj, key))
|
||||||
|
iterator.call(context, obj[key], key, obj);
|
||||||
|
// var keys = _.keys(obj), l = keys.length;
|
||||||
|
// for (var i=0; i<l; i++) iterator.call(context, obj[keys[i]], keys[i], obj);
|
||||||
}
|
}
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
if (e != breaker) throw e;
|
if (e != breaker) throw e;
|
||||||
@@ -639,12 +642,17 @@
|
|||||||
_.methods = _.functions;
|
_.methods = _.functions;
|
||||||
|
|
||||||
// ------------------------ Setup the OOP Wrapper: --------------------------
|
// ------------------------ Setup the OOP Wrapper: --------------------------
|
||||||
|
_.buildWrapper = function () { throw "Call _.initWrapper() to enable OO wrapping" }
|
||||||
|
|
||||||
|
_.initWrapper = function () {
|
||||||
// If Underscore is called as a function, it returns a wrapped object that
|
// 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
|
// can be used OO-style. This wrapper holds altered versions of all the
|
||||||
// underscore functions. Wrapped objects may be chained.
|
// underscore functions. Wrapped objects may be chained.
|
||||||
var wrapper = function(obj) { this._wrapped = obj; };
|
var wrapper = function(obj) { this._wrapped = obj; };
|
||||||
|
|
||||||
|
// Overwrite method called from _()
|
||||||
|
_.buildWrapper = function (obj) { return new wrapper(obj) };
|
||||||
|
|
||||||
// Helper function to continue chaining intermediate results.
|
// Helper function to continue chaining intermediate results.
|
||||||
var result = function(obj, chain) {
|
var result = function(obj, chain) {
|
||||||
return chain ? _(obj).chain() : obj;
|
return chain ? _(obj).chain() : obj;
|
||||||
@@ -687,5 +695,7 @@
|
|||||||
wrapper.prototype.value = function() {
|
wrapper.prototype.value = function() {
|
||||||
return this._wrapped;
|
return this._wrapped;
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
_.initWrapper();
|
||||||
})();
|
})();
|
||||||
|
|||||||
Reference in New Issue
Block a user