2024-03-26 16:32:53 +01:00

28 lines
1.9 KiB
Plaintext

== Why is this an issue?
JavaScript has a prototypal inheritance model. Each object has an internal property that points to another object, called a `prototype`. That prototype object has a prototype of its own, and the whole sequence is called a *prototype chain*. When accessing a property or a method of an object, if it is not found at the top level, the search continues through the object's prototype and then further down the prototype chain. This feature allows for very powerful dynamic inheritance patterns but can also lead to confusion when compared to the classic inheritance.
To simplify the access to the prototype of an object some browsers introduced the ``++__proto__++`` property, which was later deprecated and removed from the language. The current ECMAScript standard includes `Object.getPrototypeOf` and `Object.setPrototypeOf` static methods that should be used instead of the ``++__proto__++`` property.
[source,javascript,diff-id=1,diff-type=noncompliant]
----
let prototype = foo.__proto__; // Noncompliant: use Object.getPrototypeOf
foo.__proto__ = bar; // Noncompliant: use Object.setPrototypeOf
----
To fix your code replace ``++__proto__++`` with calls to `Object.getPrototypeOf` and `Object.setPrototypeOf` static methods.
[source,javascript,diff-id=1,diff-type=compliant]
----
let prototype = Object.getPrototypeOf(foo);
Object.setPrototypeOf(foo, bar);
----
== Resources
=== Documentation
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain[inheritance and the prototype chain]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto[\\__proto__]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getPrototypeOf[Object.getPrototypeOf]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf[Object.setPrototypeOf]