28 lines
1.1 KiB
Plaintext
28 lines
1.1 KiB
Plaintext
Prototypes of builtin objects should not be modified.
|
|
|
|
== Why is this an issue?
|
|
|
|
By default, JavaScript allows you to modify native object prototypes, such as `Array`, `String`, `Object`, and so on. This means you can add new properties or methods to native objects or override existing ones. While this flexibility can be useful in some instances, it can lead to unexpected behavior, bugs, and compatibility issues.
|
|
|
|
The rule forbids extending or modifying native JavaScript objects or prototypes.
|
|
|
|
== How to fix it
|
|
|
|
Prototypes of builtin objects should not be modified altogether.
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,text,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
Object.prototype.universe = 42;
|
|
Object.defineProperty(Array.prototype, "size", { value: 0 });
|
|
----
|
|
|
|
== Resources
|
|
=== Documentation
|
|
|
|
* https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object_prototypes[MDN - Object prototypes]
|
|
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty[MDN - ``++Object.defineProperty()++``]
|