noProto
¥Summary
-
规则生效日期:
v2.3.8¥Rule available since:
v2.3.8 -
诊断类别:
lint/nursery/noProto¥Diagnostic Category:
lint/nursery/noProto -
此规则没有修复方案。
¥This rule doesn’t have a fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
来源:
¥Sources:
¥How to configure
{ "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.prototype 的 null 原型对象,也不适用于通过 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
Biome v2.1 中文网 - 粤ICP备13048890号