Skip to content

Commit

Permalink
Fixes jashkenas#733 -- quick-fix implementation of isEqual on naked o…
Browse files Browse the repository at this point in the history
…bjects from different frames.
  • Loading branch information
jashkenas committed Sep 19, 2012
1 parent d7fdfa7 commit 4efca3e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 4 additions & 0 deletions test/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,9 @@ $(document).ready(function() {
ok(_.isEqual(isEqualObjClone, isEqualObj), 'Commutative equality is implemented for objects with custom `isEqual` methods');
ok(!_.isEqual(isEqualObj, {}), 'Objects that do not implement equivalent `isEqual` methods are not equal');
ok(!_.isEqual({}, isEqualObj), 'Commutative equality is implemented for objects with different `isEqual` methods');

// Objects from another frame.
ok(_.isEqual({}, iObject));
});

test("isEmpty", function() {
Expand Down Expand Up @@ -378,6 +381,7 @@ $(document).ready(function() {
parent.iNull = null;\
parent.iBoolean = new Boolean(false);\
parent.iUndefined = undefined;\
parent.iObject = {};\
</script>"
);
iDoc.close();
Expand Down
9 changes: 7 additions & 2 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -847,8 +847,13 @@
}
}
} else {
// Objects with different constructors are not equivalent.
if ('constructor' in a != 'constructor' in b || a.constructor != b.constructor) return false;
// Objects with different constructors are not equivalent, but `Object`s
// from different frames are.
var aCtor = a.constructor, bCtor = b.constructor;
if (aCtor !== bCtor && !(_.isFunction(aCtor) && (aCtor instanceof aCtor) &&
_.isFunction(bCtor) && (bCtor instanceof bCtor))) {
return false;
}
// Deep compare objects.
for (var key in a) {
if (_.has(a, key)) {
Expand Down

0 comments on commit 4efca3e

Please sign in to comment.