Javascript 如何在以下逻辑中访问 Object.prototype 方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39282873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:25:33  来源:igfitidea点击:

How do I access the Object.prototype method in the following logic?

javascriptecmascript-6eslint

提问by booYah

I am using the following logic to get the i18n string of the given key.

我使用以下逻辑来获取给定键的 i18n 字符串。

export function i18n(key) {
  if (entries.hasOwnProperty(key)) {
    return entries[key];
  } else if (typeof (Canadarm) !== 'undefined') {
    try {
      throw Error();
    } catch (e) {
      Canadarm.error(entries['dataBuildI18nString'] + key, e);
    }
  }
  return entries[key];
}

I am using ESLint in my project. I am getting the following error:

我在我的项目中使用 ESLint。我收到以下错误:

Do not access Object.prototype method 'hasOwnProperty' from target object. It is a 'no-prototype-builtins' error.

不要从目标对象访问 Object.prototype 方法“hasOwnProperty”。这是一个“无原型内置”错误。

How do I change my code to resolve this error ? I don't want to disable this rule.

如何更改我的代码以解决此错误?我不想禁用此规则。

回答by Oriol

You can access it via Object.prototype:

您可以通过Object.prototype以下方式访问它:

Object.prototype.hasOwnProperty.call(obj, prop);

That should be safer, because

那应该更安全,因为

  • Not all objects inherit from Object.prototype
  • Even for objects which inherit from Object.prototype, the hasOwnPropertymethod could be shadowed by something else.
  • 并非所有对象都继承自 Object.prototype
  • 即使对于从 继承的对象Object.prototype,该hasOwnProperty方法也可能被其他东西遮蔽。

Of course, the code above assumes that

当然,上面的代码假设

  • The global Objecthas not been shadowed or redefined
  • The native Object.prototype.hasOwnPropertyhas not been redefined
  • No callown property has been added to Object.prototype.hasOwnProperty
  • The native Function.prototype.callhas not been redefined
  • 全球Object没有被遮蔽或重新定义
  • 原生Object.prototype.hasOwnProperty没有被重新定义
  • 没有call自己的财产被添加到Object.prototype.hasOwnProperty
  • 原生Function.prototype.call没有被重新定义

If any of these does not hold, attempting to code in a safer way, you could have broken your code!

如果其中任何一个不成立,尝试以更安全的方式进行编码,您可能已经破坏了您的代码!

Another approach which does not need callwould be

另一种不需要的call方法是

!!Object.getOwnPropertyDescriptor(obj, prop);

回答by Mike Mathew

It seems like this would also work:

这似乎也可以工作:

key in entries

key in entries

since that will return a boolean on whether or not the key exists inside the object?

因为这将返回一个关于键是否存在于对象内部的布尔值?

回答by xameeramir

For your specific case, the following examples shall work:

对于您的具体情况,以下示例应有效:

if(Object.prototype.hasOwnProperty.call(entries, "key")) {
    //rest of the code
}

OR

或者

if(Object.prototype.isPrototypeOf.call(entries, key)) {
    //rest of the code
}

OR

或者

if({}.propertyIsEnumerable.call(entries, "key")) {
    //rest of the code
}

回答by Albert James Teddy

I hope I won't get downvoted for this, probably will, but !

我希望我不会因此而被低估,可能会,但是!

var a = {b: "I'm here"}
if (a["b"]) { console.log(a["b"]) }
if (a["c"]) { console.log("Never going to happen") }

Has, insofar, never broken my code But I'm not sure if it is the case in all web browsers...

到目前为止,从未破坏过我的代码但我不确定所有网络浏览器是否都是这种情况......

(Also, if Canadarmis undefined, your code seem to return entries[key];even if key is not in entries...)

(另外,如果Canadarm未定义,return entries[key];即使键不在条目中,您的代码似乎也是......)

回答by S..

Use the null coalescing operator: if (entries?.key) {

使用空合并运算符: if (entries?.key) {