noProto
Summary
Section titled “Summary”- Rule available since:
v2.3.8 - Diagnostic Category:
lint/nursery/noProto - This rule doesn’t have a fix.
- The default severity of this rule is information.
- Sources:
- Same as
no-proto
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noProto": "error" } } }}Description
Section titled “Description”Disallow the use of the deprecated __proto__ object property.
Object.prototype.__proto__
is a special accessor used to get or set the prototype of an object. \
However, it has been deprecated since ECMAScript 2009, being much slower and much less reliable than its
modern counterparts Object.getPrototypeOf()
and Object.setPrototypeOf().
Since it is a regular property on Object.prototype,
__proto__ will not work on null-prototype objects that do not extend from Object.prototype
nor ones having created their own __proto__ properties via Object.defineProperty.
As such, this rule encourages the use of Object.getPrototypeOf() and Object.setPrototypeOf()
in lieu of directly accessing __proto__.
Note that this does not check for the use of __proto__ inside object literal definitions
to set a newly created object’s prototype,
which is standard practice and well-optimized in modern browsers.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”obj.__proto__ = a;code-block.js:1:1 lint/nursery/noProto ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Avoid use of the deprecated __proto__ accessor.
> 1 │ obj.__proto__ = a;
│ ^^^^^^^^^^^^^
2 │
ℹ Object.prototype.__proto__ is an outdated way to get or set an object’s prototype,
having been deprecated in 2009 for being inefficient and unreliable.
ℹ Object.getPrototypeOf() and Object.setPrototypeOf() are modern alternatives that work on all objects and are more performant.
const b = obj.__proto__;code-block.js:1:11 lint/nursery/noProto ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Avoid use of the deprecated __proto__ accessor.
> 1 │ const b = obj.__proto__;
│ ^^^^^^^^^^^^^
2 │
ℹ Object.prototype.__proto__ is an outdated way to get or set an object’s prototype,
having been deprecated in 2009 for being inefficient and unreliable.
ℹ Object.getPrototypeOf() and Object.setPrototypeOf() are modern alternatives that work on all objects and are more performant.
const a = Object.getPrototypeOf(obj);Object.setPrototypeOf(obj, b);// This sets `foo`'s prototype to `null` (similar to `Object.create`), and is// well-defined across browsers.const foo = { __proto__: null, a: 1,}Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.