From 26a30551f912f8180e6c2381d0eae4b24259fb70 Mon Sep 17 00:00:00 2001 From: David Glasser Date: Mon, 19 Nov 2012 15:53:42 -0800 Subject: [PATCH] toArray: only call Array.prototype.slice on actual arrays. Specifically, this fixes _.toArray on NodeList objects on IE8, which worked in Underscore 1.3.3 but throws "JScript object expected" in 1.4.0. --- test/collections.js | 3 +++ underscore.js | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/test/collections.js b/test/collections.js index 2e684056f..2cb3f6b67 100644 --- a/test/collections.js +++ b/test/collections.js @@ -418,6 +418,9 @@ $(document).ready(function() { var numbers = _.toArray({one : 1, two : 2, three : 3}); equal(numbers.join(', '), '1, 2, 3', 'object flattened into array'); + + // _.toArray on a NodeList should not throw. + ok(_.isArray(_.toArray(document.childNodes))); }); test('size', function() { diff --git a/underscore.js b/underscore.js index 3771cb422..647e62aa2 100644 --- a/underscore.js +++ b/underscore.js @@ -358,7 +358,8 @@ // Safely convert anything iterable into a real, live array. _.toArray = function(obj) { if (!obj) return []; - if (obj.length === +obj.length) return slice.call(obj); + if (_.isArray(obj)) return slice.call(obj); + if (obj.length === +obj.length) return _.map(obj, _.identity); return _.values(obj); };