Skip to content

Commit de3a016

Browse files
author
Grahame Bowland
committed
backport angular.copy fix from 1.3.x branch
backported commit: angular@b59b04f which is a fix to: angular#4996
1 parent 2a8493f commit de3a016

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

src/Angular.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,8 @@ function copy(source, destination, stackSource, stackDest) {
783783
} else if (isRegExp(source)) {
784784
destination = new RegExp(source.source);
785785
} else if (isObject(source)) {
786-
destination = copy(source, {}, stackSource, stackDest);
786+
var emptyObject = Object.create(Object.getPrototypeOf(source));
787+
destination = copy(source, emptyObject, stackSource, stackDest);
787788
}
788789
}
789790
} else {
@@ -818,12 +819,14 @@ function copy(source, destination, stackSource, stackDest) {
818819
delete destination[key];
819820
});
820821
for ( var key in source) {
821-
result = copy(source[key], null, stackSource, stackDest);
822-
if (isObject(source[key])) {
823-
stackSource.push(source[key]);
824-
stackDest.push(result);
822+
if(source.hasOwnProperty(key)) {
823+
result = copy(source[key], null, stackSource, stackDest);
824+
if (isObject(source[key])) {
825+
stackSource.push(source[key]);
826+
stackDest.push(result);
827+
}
828+
destination[key] = result;
825829
}
826-
destination[key] = result;
827830
}
828831
setHashKey(destination,h);
829832
}

test/AngularSpec.js

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

27+
it("should preserve prototype chaining", function() {
28+
var GrandParentProto = {};
29+
var ParentProto = Object.create(GrandParentProto);
30+
var obj = Object.create(ParentProto);
31+
expect(ParentProto.isPrototypeOf(copy(obj))).toBe(true);
32+
expect(GrandParentProto.isPrototypeOf(copy(obj))).toBe(true);
33+
var Foo = function() {};
34+
expect(copy(new Foo()) instanceof Foo).toBe(true);
35+
});
36+
2737
it("should copy Date", function() {
2838
var date = new Date(123);
2939
expect(copy(date) instanceof Date).toBeTruthy();

0 commit comments

Comments
 (0)