Skip to content

Commit 484f0d1

Browse files
iddingsBryan.Iddings
and
Bryan.Iddings
authored
Preserve object prototypes when cloning (#7404)
Co-authored-by: Bryan.Iddings <[email protected]>
1 parent 2df6986 commit 484f0d1

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/helpers/helpers.core.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ var helpers = {
175175
}
176176

177177
if (helpers.isObject(source)) {
178-
var target = {};
178+
var target = Object.create(source);
179179
var keys = Object.keys(source);
180180
var klen = keys.length;
181181
var k = 0;

test/specs/helpers.core.tests.js

+19
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,25 @@ describe('Chart.helpers.core', function() {
301301
expect(output.o.a).not.toBe(a1);
302302
expect(output.a).not.toBe(a0);
303303
});
304+
it('should preserve prototype of objects', function() {
305+
// https://github.com/chartjs/Chart.js/issues/7340
306+
function MyConfigObject(s) {
307+
this._s = s;
308+
}
309+
MyConfigObject.prototype.func = function() {
310+
return 10;
311+
};
312+
var original = new MyConfigObject('something');
313+
var output = helpers.merge({}, {
314+
plugins: [{
315+
test: original
316+
}]
317+
});
318+
var clone = output.plugins[0].test;
319+
expect(clone).toBeInstanceOf(MyConfigObject);
320+
expect(clone).toEqual(original);
321+
expect(clone === original).toBeFalse();
322+
});
304323
});
305324

306325
describe('merge', function() {

0 commit comments

Comments
 (0)