diff --git a/index.html b/index.html index eacb7016e..2eb2dbc31 100644 --- a/index.html +++ b/index.html @@ -813,6 +813,18 @@ _.isElement(jQuery('body')[0]); => false _.isArray([1,2,3]); => true + + +

+ isArguments_.isArguments(object) +
+ Returns true if object is an Arguments object. +

+
+(function(){ return _.isArguments(arguments); })(1, 2, 3);
+=> true
+_.isArguments([1,2,3]);
+=> false
 

diff --git a/test/collections.js b/test/collections.js index 47ca9d430..935edcb95 100644 --- a/test/collections.js +++ b/test/collections.js @@ -29,6 +29,10 @@ $(document).ready(function() { answer = null; _.each([1, 2, 3], function(num, index, arr){ if (_.include(arr, num)) answer = true; }); ok(answer, 'can reference the original collection from inside the iterator'); + + answers = []; + _.each({range : 1, speed : 2, length : 3}, function(v){ answers.push(v); }); + ok(answers.join(', '), '1, 2, 3', 'can iterate over objects with numeric length properties'); }); test('collections: map', function() { diff --git a/test/objects.js b/test/objects.js index 8ba67858e..cd45c0bc6 100644 --- a/test/objects.js +++ b/test/objects.js @@ -14,7 +14,7 @@ $(document).ready(function() { var expected = ["all", "any", "bind", "bindAll", "breakLoop", "clone", "compact", "compose","defer", "delay", "detect", "each", "every", "extend", "filter", "first", "flatten", "foldl", "foldr", "forEach", "functions", "head", "identity", "include", - "indexOf", "inject", "intersect", "invoke", "isArray", "isDate", "isElement", "isEmpty", "isEqual", + "indexOf", "inject", "intersect", "invoke", "isArguments", "isArray", "isDate", "isElement", "isEmpty", "isEqual", "isFunction", "isNaN", "isNull", "isNumber", "isRegExp", "isString", "isUndefined", "keys", "last", "lastIndexOf", "map", "max", "methods", "min", "noConflict", "pluck", "range", "reduce", "reduceRight", "reject", "rest", "select", "size", "some", "sortBy", "sortedIndex", "tail", "template", "toArray", "uniq", @@ -71,6 +71,13 @@ $(document).ready(function() { ok(_.isElement($('html')[0]), 'the html tag is a DOM element'); }); + test("objects: isArguments", function() { + var args = (function(){ return arguments; })(1, 2, 3); + ok(_.isArguments(args), 'the arguments object is an arguments object'); + ok(!_.isArguments(_.toArray(args)), 'but not when it\'s converted into an array'); + ok(!_.isArguments([1,2,3]), 'and not vanilla arrays.'); + }); + test("objects: isArray", function() { ok(!_.isArray(arguments), 'the arguments object is not an array'); ok(_.isArray([1, 2, 3]), 'but arrays are'); diff --git a/underscore.js b/underscore.js index 7ae664386..810531f72 100644 --- a/underscore.js +++ b/underscore.js @@ -30,8 +30,12 @@ // Export the Underscore object for CommonJS. if (typeof exports !== 'undefined') exports._ = _; - // Create quick reference variables for speed access to Object.prototype. - var toString = Object.prototype.toString, hasOwnProperty = Object.prototype.hasOwnProperty; + // Create quick reference variables for speed access to core prototypes. + var slice = Array.prototype.slice, + unshift = Array.prototype.unshift, + toString = Object.prototype.toString, + hasOwnProperty = Object.prototype.hasOwnProperty, + propertyIsEnumerable = Object.prototype.propertyIsEnumerable; // Current version. _.VERSION = '0.5.0'; @@ -45,7 +49,7 @@ try { if (obj.forEach) { obj.forEach(iterator, context); - } else if (_.isNumber(obj.length)) { + } else if (_.isArray(obj) || _.isArguments(obj)) { for (var i=0, l=obj.length; i