Index

no-proto

禁止使用 __proto__ 属性

__proto__ 属性从 ECMAScript 3.1 开始已被弃用,不应在代码中使用。请改用 Object.getPrototypeOfObject.setPrototypeOf

规则详情

🌐 Rule Details

当使用 new 操作符创建对象时,__proto__ 被设置为对象构造函数的原始“prototype”属性。Object.getPrototypeOf 是获取对象原型的首选方法。要更改对象的原型,请使用 Object.setPrototypeOf

🌐 When an object is created with the new operator, __proto__ is set to the original “prototype” property of the object’s constructor function. Object.getPrototypeOf is the preferred method of getting the object’s prototype. To change an object’s prototype, use Object.setPrototypeOf.

此规则的错误代码示例:

🌐 Examples of incorrect code for this rule:

在线运行
/*eslint no-proto: "error"*/

const a = obj.__proto__;

const a1 = obj["__proto__"];

obj.__proto__ = b;

obj["__proto__"] = b;

符合此规则的正确代码示例:

🌐 Examples of correct code for this rule:

在线运行
/*eslint no-proto: "error"*/

const a = Object.getPrototypeOf(obj);

Object.setPrototypeOf(obj, b);

const c = { __proto__: a };

选项

🌐 Options

此规则没有选项。

🌐 This rule has no options.

何时不使用

🌐 When Not To Use It

如果你需要支持实现了 __proto__ 属性但不支持 Object.getPrototypeOfObject.setPrototypeOf 的旧版浏览器,你可能想要关闭这个规则。

🌐 You might want to turn this rule off if you need to support legacy browsers which implement the __proto__ property but not Object.getPrototypeOf or Object.setPrototypeOf.

版本

此规则是在 ESLint v0.0.9 中引入。

进阶读物

资源