diff --git a/underscore.js b/underscore.js index bd74fbcf5..a445cdcb0 100644 --- a/underscore.js +++ b/underscore.js @@ -37,6 +37,25 @@ hasOwnProperty = Object.prototype.hasOwnProperty, propertyIsEnumerable = Object.prototype.propertyIsEnumerable; + // All native implementations we hope to use are declared here, + // mostly Array.prototype methods. + // + // '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: Array.prototype.forEach, + map: Array.prototype.map, + reduce: Array.prototype.reduce, + reduceRight: Array.prototype.reduceRight, + filter: Array.prototype.filter, + every: Array.prototype.every, + some: Array.prototype.some, + indexOf: Array.prototype.indexOf, + lastIndexOf: Array.prototype.lastIndexOf, + isArray: Array.isArray, + keys: Object.keys + }; + // Current version. _.VERSION = '0.5.8'; @@ -49,7 +68,7 @@ _.forEach = function(obj, iterator, context) { var index = 0; try { - if (obj.forEach) { + if (obj.forEach === Native.forEach) { obj.forEach(iterator, context); } else if (_.isNumber(obj.length)) { for (var i=0, l=obj.length; i