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 (v2) #7404

Merged
merged 1 commit into from
Jun 4, 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 @@ -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