Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 12d77c4

Browse files
mernenpetebacondarwin
authored andcommitted
fix(angular.equals): do not match keys defined in the prototype chain
Merely testing for object[key] will give incorrect results on keys defined in Object.prototype. Note: IE8 is generally broken in this regard since `for...in` never returns certain property keys even if they are defined directly on the object. See #2141 - partially merges this PR
1 parent d88dc4a commit 12d77c4

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/Angular.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
////////////////////////////////////
44

5+
/**
6+
* hasOwnProperty may be overriden by a property of the same name, or entirely
7+
* absent from an object that does not inherit Object.prototype; this copy is
8+
* used instead
9+
*/
10+
var hasOwnProperty = function(obj, key) {
11+
return Object.prototype.hasOwnProperty.call(obj, key);
12+
};
13+
514
/**
615
* @ngdoc function
716
* @name angular.lowercase
@@ -685,7 +694,7 @@ function equals(o1, o2) {
685694
keySet[key] = true;
686695
}
687696
for(key in o2) {
688-
if (!keySet[key] &&
697+
if (!keySet.hasOwnProperty(key) &&
689698
key.charAt(0) !== '$' &&
690699
o2[key] !== undefined &&
691700
!isFunction(o2[key])) return false;

test/AngularSpec.js

+8
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,14 @@ describe('angular', function() {
270270
expect(equals(new Date(0), 0)).toBe(false);
271271
expect(equals(0, new Date(0))).toBe(false);
272272
});
273+
274+
it('should correctly test for keys that are present on Object.prototype', function() {
275+
// MS IE8 just doesn't work for this kind of thing, since "for ... in" doesn't return
276+
// things like hasOwnProperty even if it is explicitly defined on the actual object!
277+
if (msie<=8) return;
278+
expect(equals({}, {hasOwnProperty: 1})).toBe(false);
279+
expect(equals({}, {toString: null})).toBe(false);
280+
});
273281
});
274282

275283
describe('size', function() {

0 commit comments

Comments
 (0)