Skip to content

Commit

Permalink
Preserve object prototypes when cloning (#7404)
Browse files Browse the repository at this point in the history
Co-authored-by: Bryan.Iddings <[email protected]>
  • Loading branch information
iddings and Bryan.Iddings authored Jun 4, 2020
1 parent 2df6986 commit 484f0d1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/helpers/helpers.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ var helpers = {
}

if (helpers.isObject(source)) {
var target = {};
var target = Object.create(source);
var keys = Object.keys(source);
var klen = keys.length;
var k = 0;
Expand Down
19 changes: 19 additions & 0 deletions test/specs/helpers.core.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,25 @@ describe('Chart.helpers.core', function() {
expect(output.o.a).not.toBe(a1);
expect(output.a).not.toBe(a0);
});
it('should preserve prototype of objects', function() {
// https://github.com/chartjs/Chart.js/issues/7340
function MyConfigObject(s) {
this._s = s;
}
MyConfigObject.prototype.func = function() {
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('merge', function() {
Expand Down

0 comments on commit 484f0d1

Please sign in to comment.