no-prototype-builtins
NOTE: this rule is part of the
推荐 rule set.Enable full set in
deno.json:{
"lint": {
"rules": {
"tags": ["推荐"]
}
}
}Enable full set using the Deno CLI:
deno lint --rules-tags=推荐
This rule can be explictly included to or excluded from the rules present in the current tag by adding it to the
include or exclude array in deno.json:{
"lint": {
"rules": {
"include": ["no-prototype-builtins"],
"exclude": ["no-prototype-builtins"]
}
}
}不允许直接使用 Object.prototype 的内置方法。
如果通过 Object.create(null) 创建对象,则这些对象没有指定原型。这可能会导致运行时错误,因为你假设对象具有来自 Object.prototype 的属性,并尝试调用以下方法:
hasOwnPropertyisPrototypeOfpropertyIsEnumerable
因此,始终建议显式地从 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");