Skip to content

noProto

¥Summary

¥How to configure

biome.json
{
"linter": {
"rules": {
"nursery": {
"noProto": "error"
}
}
}
}

¥Description

禁止使用已弃用的 __proto__ 对象属性。

¥Disallow the use of the deprecated __proto__ object property.

Object.prototype.__proto__ 是一个特殊的访问器,用于获取或设置对象的原型。\

¥Object.prototype.__proto__ is a special accessor used to get or set the prototype of an object. \

但是,自 ECMAScript 2009 以来,它已被弃用,与现代的 Object.getPrototypeOf()Object.setPrototypeOf() 相比,速度慢得多,可靠性也低得多。

¥However, it has been deprecated since ECMAScript 2009, being much slower and much less reliable than its modern counterparts Object.getPrototypeOf() and Object.setPrototypeOf().

由于 __proto__Object.prototype 上的常规属性,因此它不适用于未继承自 Object.prototypenull 原型对象,也不适用于通过 Object.defineProperty 创建自身 __proto__ 属性的对象。

¥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.

因此,此规则鼓励使用 Object.getPrototypeOf()Object.setPrototypeOf(),而不是直接访问 __proto__

¥As such, this rule encourages the use of Object.getPrototypeOf() and Object.setPrototypeOf() in lieu of directly accessing __proto__.

请注意,此方法不会检查对象字面量定义中是否使用了 __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

¥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.

¥Valid

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