no-proto
禁止使用 __proto__
属性
__proto__
属性自 ECMAScript 3.1 起已被弃用,不应在代码中使用。请改用 Object.getPrototypeOf
和 Object.setPrototypeOf
。
¥__proto__
property has been deprecated as of ECMAScript 3.1 and shouldn’t be used in the code. Use Object.getPrototypeOf
and Object.setPrototypeOf
instead.
规则详情
¥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"*/
var a = obj.__proto__;
var a = obj["__proto__"];
obj.__proto__ = b;
obj["__proto__"] = b;
此规则的正确代码示例:
¥Examples of correct code for this rule:
/*eslint no-proto: "error"*/
var a = Object.getPrototypeOf(obj);
Object.setPrototypeOf(obj, b);
var c = { __proto__: a };
何时不使用
¥When Not To Use It
如果你需要支持实现 __proto__
属性但不支持 Object.getPrototypeOf
或 Object.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 中引入。