question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Preserve object properties when cloning and when those properties have getters/setters

See original GitHub issue

Expected Behavior

When a non-standard property is included with options, that non-standard property is cloned in such a way that its properties are preserved on the object, even when its properties feature getters and setters.

Current Behavior

When a non-standard property of options has getters and setters, the clone helper method does not preserve that property’s keys on the property itself - instead, it only copies them to the property’s prototype.

This appears to stem from a change that was made in 2.9.4. The method of cloning an object was modified, such that it now utilizes Object.create(), which copies the properties from the source object into the target object’s prototype. This is a problem if an object’s properties includes getters and setters (which would include, for example, any Vue.js observer); as per https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain:

Setting a property to an object creates an own property. The only exception to the getting and setting behavior rules is when there is an inherited property with a getter or a setter.

Possible Solution

It seems like the suggested solution in https://github.com/chartjs/Chart.js/issues/7340 (a related PR of which led to this issue) should be sufficient.

Steps to Reproduce

Please see https://codepen.io/nb1987/pen/LYbGzNW - in particular, the non-standard properties added to options. From the console log, you can see that Object.keys(myChart.options.nonStandardProp) returns ['a'], but Object.keys(myChart.options.nonStandardPropWithGettersAndSetters) returns an empty array.

Context

I am using https://github.com/apertureless/vue-chartjs and https://github.com/chartjs/chartjs-plugin-annotation, and the annotation plugin is not working due to this issue. It is easy enough to fix the issue by ensuring that the non-standard option (in this case, annotation) is supplied as a POJO instead of as a Vue observer (e.g., using Object.assign({ }, options)), but it’s a problem that could potentially affect users outside the context of Vue and the chartjs-plugin-annotation.

Others have reported the issue at:

Environment

  • Chart.js version: 2.9.4
  • Browser name and version: Chrome 87.0.4280.141
  • Link to your project:

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
LeeLenaleeecommented, Nov 27, 2021

@jastax no there won’t be a version 2.9.5 but the issue says that it is patched in version 2.9.4 so you can use that

0reactions
fberger-cscommented, Nov 27, 2021

up

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to clone a JavaScript object including getters and setters?
The now most obvious solution to me seems to firstly use the JSON trick above to make obj2 have all of obj1 's...
Read more >
Property getters and setters - The Modern JavaScript Tutorial
Accessor properties are represented by “getter” and “setter” methods. ... For instance, we have a user object with name and surname :.
Read more >
Copying properties from one object to another (including ...
Object.assign is the standard way to copy properties from one object to another. It is often used for copying properties that are one-layer ......
Read more >
Data preservation on cloning target instances
You can use data preservers to protect data on the target instance from being overwritten. If you have custom applications, you must also ......
Read more >
14. New OOP features besides classes - Exploring JS
In ECMAScript 5, methods are properties whose values are functions: ... One last use case for Object.assign() is a quick way of cloning...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found