Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve object prototypes when cloning #7381

Merged
merged 1 commit into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/helpers/helpers.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 21 additions & 0 deletions test/specs/helpers.core.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down