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

Commit d64350d

Browse files
committed
fix(ng): in angular.copy do not call hasOwnProperty on objects with not prototype
angular.copy can be called with an object that has no prototype. Call Object.prototype.hasOwnProperty instead of object.hasOwnProperty to safely check if object has a property
1 parent 2b41a58 commit d64350d

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/Angular.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ function forEach(obj, iterator, context) {
262262
obj.forEach(iterator, context, obj);
263263
} else {
264264
for (key in obj) {
265-
if (obj.hasOwnProperty(key)) {
265+
if (hasOwnProperty.call(obj, key)) {
266266
iterator.call(context, obj[key], key, obj);
267267
}
268268
}
@@ -754,7 +754,7 @@ function copy(source, destination, stackSource, stackDest) {
754754
});
755755
}
756756
for (var key in source) {
757-
if (source.hasOwnProperty(key)) {
757+
if (hasOwnProperty.call(source, key)) {
758758
result = copy(source[key], null, stackSource, stackDest);
759759
if (isObject(source[key])) {
760760
stackSource.push(source[key]);
@@ -854,7 +854,7 @@ function equals(o1, o2) {
854854
keySet[key] = true;
855855
}
856856
for (key in o2) {
857-
if (!keySet.hasOwnProperty(key) &&
857+
if (!hasOwnProperty.call(keySet, key) &&
858858
key.charAt(0) !== '$' &&
859859
o2[key] !== undefined &&
860860
!isFunction(o2[key])) return false;

test/AngularSpec.js

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ describe('angular', function() {
2424
expect(copy([], arr)).toBe(arr);
2525
});
2626

27+
it("should copy objects with no prototype", function() {
28+
var obj = Object.create(null);
29+
obj.value = 0;
30+
expect(copy({}, obj)).toBe(obj);
31+
});
32+
2733
it("should preserve prototype chaining", function() {
2834
var GrandParentProto = {};
2935
var ParentProto = Object.create(GrandParentProto);

0 commit comments

Comments
 (0)