From: Bryan Iddings Date: Thu, 4 Jun 2020 21:23:59 +0000 (-0400) Subject: Preserve object prototypes when cloning (#7404) X-Git-Tag: v2.9.4~8 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=484f0d1e518963436d5013f61001558ef9788edf;p=thirdparty%2FChart.js.git Preserve object prototypes when cloning (#7404) Co-authored-by: Bryan.Iddings --- diff --git a/src/helpers/helpers.core.js b/src/helpers/helpers.core.js index 350a20750..6cf28c00d 100644 --- a/src/helpers/helpers.core.js +++ b/src/helpers/helpers.core.js @@ -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; diff --git a/test/specs/helpers.core.tests.js b/test/specs/helpers.core.tests.js index cdda01b96..1f524089a 100644 --- a/test/specs/helpers.core.tests.js +++ b/test/specs/helpers.core.tests.js @@ -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() {