diff --git a/underscore.js b/underscore.js index 7b48ab101..901f69594 100644 --- a/underscore.js +++ b/underscore.js @@ -26,12 +26,12 @@ if (obj.forEach) { obj.forEach(iterator, context); } else if (obj.length) { - for (var i=0; i= result.computed) result = {value : value, computed : computed}; + computed >= result.computed && (result = {value : value, computed : computed}); }); return result.value; }; @@ -164,10 +161,10 @@ // Return the minimum element (or element-based computation). _.min = function(obj, iterator, context) { if (!iterator && _.isArray(obj)) return Math.min.apply(Math, obj); - var result; + var result = {computed : Infinity}; _.each(obj, function(value, index) { var computed = iterator ? iterator.call(context, value, index) : value; - if (result == null || computed < result.computed) result = {value : value, computed : computed}; + computed < result.computed && (result = {value : value, computed : computed}); }); return result.value; }; @@ -276,15 +273,16 @@ // item in an array, or -1 if the item is not included in the array. _.indexOf = function(array, item) { if (array.indexOf) return array.indexOf(item); - for (i=0; i=0; i--) if (array[i] === item) return i; + var i = array.length; + while (i--) if (array[i] === item) return i; return -1; }; @@ -403,7 +401,7 @@ // Is a given value a Function? _.isFunction = function(obj) { - return typeof obj == 'function'; + return Object.prototype.toString.call(obj) == '[object Function]'; }; // Is a given variable undefined? @@ -427,8 +425,8 @@ return prefix ? prefix + id : id; }; - // Javascript templating a-la ERB, pilfered from John Resig's - // "Secrets of the Javascript Ninja", page 83. + // JavaScript templating a-la ERB, pilfered from John Resig's + // "Secrets of the JavaScript Ninja", page 83. _.template = function(str, data) { var fn = new Function('obj', 'var p=[],print=function(){p.push.apply(p,arguments);};' +