禁止直接使用原型链上的内置方法
注意:此规则是
recommended
规则集的一部分。在
deno.json
中启用完整集合{ "lint": { "rules": { "tags": ["recommended"] } } }
使用 Deno CLI 启用完整集合
deno lint --rules-tags=recommended
此规则可以通过将其添加到
deno.json
中的 include
或 exclude
数组,从而明确地包含或排除在当前标签中的规则之外。{ "lint": { "rules": { "include": ["no-prototype-builtins"], "exclude": ["no-prototype-builtins"] } } }
禁止直接使用 Object.prototype
内置方法。
如果对象是通过 Object.create(null)
创建的,它们将没有指定原型。当你假设对象拥有 Object.prototype
中的属性并尝试调用以下方法时,这可能会导致运行时错误:
hasOwnProperty
isPrototypeOf
propertyIsEnumerable
相反,始终建议明确地从 Object.prototype
调用这些方法。
无效示例
const a = foo.hasOwnProperty("bar");
const b = foo.isPrototypeOf("bar");
const c = foo.propertyIsEnumerable("bar");
有效示例
const a = Object.prototype.hasOwnProperty.call(foo, "bar");
const b = Object.prototype.isPrototypeOf.call(foo, "bar");
const c = Object.prototype.propertyIsEnumerable.call(foo, "bar");