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

Commit 7829c50

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 7829c50

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/Angular.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
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 hasOwnPropertyFn = Object.prototype.hasOwnProperty;
11+
var hasOwnPropertyLocal = function(obj, key) {
12+
return hasOwnPropertyFn.call(obj, key);
13+
};
14+
515
/**
616
* @ngdoc function
717
* @name angular.lowercase
@@ -685,7 +695,7 @@ function equals(o1, o2) {
685695
keySet[key] = true;
686696
}
687697
for(key in o2) {
688-
if (!keySet[key] &&
698+
if (!keySet.hasOwnProperty(key) &&
689699
key.charAt(0) !== '$' &&
690700
o2[key] !== undefined &&
691701
!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)