0

If I modify Object.prototype in a seemingly harmless way (just add any property to it), the library Chart.js cannot even load:

<script>
    Object.prototype.someNewProperty='some value'
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js'></script>

I got an error in the console: Uncaught TypeError: Cannot read property '1' of null.

If I remove the line Object.prototype.someNewProperty='some value' everything works fine.

What is the source of the problem? How Chart.js knows that I have modified Object.prototype and how this modification prevents the library from being loaded?

  • 1
    Well, just don't break the global environment. What you are doing is not harmless. Chart.js does (as many other libraries) reasonably assume that there are no enumerable properties in `Object.prototype`. – Bergi Apr 11 '18 at 12:03
  • 1
    Have a look at [How to define method in javascript on Array.prototype and Object.prototype so that it doesn't appear in for in loop](https://stackoverflow.com/q/13296340/1048572) – Bergi Apr 11 '18 at 12:04
  • Thanks, the advice about Object.defineProperty is really helpful! – Grigory Hatsevich Apr 11 '18 at 15:27

0 Answers0