eslint/no-extend-native 可疑
用途
防止使用新属性扩展诸如 对象
、字符串
或 数组
等本机全局对象。
为什么这不好?
扩展本机对象会导致意外行为并与其他代码产生冲突。
例如
js
// Adding a new property, which might seem okay
Object.prototype.extra = 55;
// Defining a user object
const users = {
1: "user1",
2: "user2",
};
for (const id in users) {
// This will print "extra" as well as "1" and "2":
console.log(id);
}
示例
本规则不正确的代码示例
js
Object.prototype.p = 0;
Object.defineProperty(Array.prototype, "p", { value: 0 });
本规则正确的代码示例
js
x.prototype.p = 0;
Object.defineProperty(x.prototype, "p", { value: 0 });