From 164e19c121b617b53734d313e44b9f386b994100 Mon Sep 17 00:00:00 2001 From: Mike Frawley Date: Tue, 23 Feb 2010 17:13:18 -0600 Subject: [PATCH] use `var native_reduceRight` etc. variables instead of `Native = { reduceRight: ... }` This way we only get the native versions of functions we use. Will make testing non-native versions as we can't just swap do `_.Native = {}`, but this wouldn't have worked for `isArray` and `keys` anyways, as these are looked up on initialization, so I think the solution is to have an init() method on underscore where we will re-initialize functions and try and use native versions then --- underscore.js | 59 ++++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 31 deletions(-) diff --git a/underscore.js b/underscore.js index 696b63f4e..609e5b567 100644 --- a/underscore.js +++ b/underscore.js @@ -32,31 +32,28 @@ if (typeof exports !== 'undefined') exports._ = _; // Save bytes in the minified (but not gzipped) version: - var ArrayPrototype = Array.prototype; + var Array_Prototype = Array.prototype; // Create quick reference variables for speed access to core prototypes. - var slice = ArrayPrototype.slice, - unshift = ArrayPrototype.unshift, + var slice = Array_Prototype.slice, + unshift = Array_Prototype.unshift, toString = Object.prototype.toString, hasOwnProperty = Object.prototype.hasOwnProperty, propertyIsEnumerable = Object.prototype.propertyIsEnumerable; // All native implementations we hope to use are declared here. - // 'native' is on the long list of reserved words. - // Ok to use as a property name, though jsLint doesn't agree. - var Native = _['native'] = { - forEach: ArrayPrototype.forEach, - map: ArrayPrototype.map, - reduce: ArrayPrototype.reduce, - reduceRight: ArrayPrototype.reduceRight, - filter: ArrayPrototype.filter, - every: ArrayPrototype.every, - some: ArrayPrototype.some, - indexOf: ArrayPrototype.indexOf, - lastIndexOf: ArrayPrototype.lastIndexOf, - isArray: Array.isArray, - keys: Object.keys - }; + var + native_forEach = Array_Prototype.forEach, + native_map = Array_Prototype.map, + native_reduce = Array_Prototype.reduce, + native_reduceRight = Array_Prototype.reduceRight, + native_filter = Array_Prototype.filter, + native_every = Array_Prototype.every, + native_some = Array_Prototype.some, + native_indexOf = Array_Prototype.indexOf, + native_lastIndexOf = Array_Prototype.lastIndexOf, + native_isArray = Array['isArray'], + native_keys = Object['keys']; // Current version. _.VERSION = '0.5.8'; @@ -70,7 +67,7 @@ _.forEach = function(obj, iterator, context) { var index = 0; try { - if (obj.forEach === Native.forEach) { + if (obj.forEach === native_forEach) { obj.forEach(iterator, context); } else if (_.isNumber(obj.length)) { for (var i=0, l=obj.length; i