Preserve object prototypes when cloning (#7381)

This commit is contained in:
Jukka Kurkela 2020-05-21 00:40:42 +03:00 committed by GitHub
parent 50f2a1097a
commit 51be344717
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View File

@ -199,7 +199,7 @@ export function clone(source) {
}
if (isObject(source)) {
const target = {};
const target = Object.create(source);
const keys = Object.keys(source);
const klen = keys.length;
let k = 0;

View File

@ -360,6 +360,27 @@ describe('Chart.helpers.core', function() {
expect(output.o).not.toBe(o0);
expect(output.o.a).not.toBe(a1);
});
it('should preserve prototype of objects', function() {
// https://github.com/chartjs/Chart.js/issues/7340
class MyConfigObject {
constructor(s) {
this._s = s;
}
func() {
return 10;
}
}
var original = new MyConfigObject('something');
var output = helpers.merge({}, {
plugins: [{
test: original
}]
});
var clone = output.plugins[0].test;
expect(clone).toBeInstanceOf(MyConfigObject);
expect(clone).toEqual(original);
expect(clone === original).toBeFalse();
});
});
describe('mergeIf', function() {