From 7fc616943e6ecc43662cb5595f2e839a2ed0172e Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Tue, 18 Oct 2011 23:26:59 -0400 Subject: [PATCH] Issue #329, special check for the common and magical 'length' property. --- test/objects.js | 2 +- underscore.js | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/test/objects.js b/test/objects.js index 5fc4b6a0a..e02a76ae4 100644 --- a/test/objects.js +++ b/test/objects.js @@ -183,7 +183,7 @@ $(document).ready(function() { // Sparse arrays. ok(_.isEqual(Array(3), Array(3)), "Sparse arrays of identical lengths are equal"); - ok(_.isEqual(Array(3), Array(6)), "Sparse arrays of different lengths are equal when both are empty"); + ok(!_.isEqual(Array(3), Array(6)), "Sparse arrays of different lengths are not equal when both are empty"); // According to the Microsoft deviations spec, section 2.1.26, JScript 5.x treats `undefined` // elements in arrays as elisions. Thus, sparse arrays and dense arrays containing `undefined` diff --git a/underscore.js b/underscore.js index 1cb07d815..d219bc206 100644 --- a/underscore.js +++ b/underscore.js @@ -675,6 +675,8 @@ } // Ensure that both values are objects. if (typeA != 'object') return false; + // Arrays or Arraylikes with different lengths are not equal. + if (a.length !== b.length) return false; // Objects with different constructors are not equal. if (a.constructor !== b.constructor) return false; // Assume equality for cyclic structures. The algorithm for detecting cyclic structures is