admin管理员组

文章数量:1129049

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

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:

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

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

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

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:

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

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

Share Improve this question edited Mar 28, 2022 at 1:58 tog22 5261 gold badge4 silver badges22 bronze badges asked Sep 2, 2016 at 0:57 booYahbooYah 3,1792 gold badges11 silver badges4 bronze badges 4
  • 26 You should probably read the docs. There are examples of correct code ~ eslint.org/docs/rules/no-prototype-builtins – Phil Commented Sep 2, 2016 at 1:01
  • 2 The code is working fine. This is a linting error. I just want to modify the syntax so that the linting rule is satisfied. – booYah Commented Sep 2, 2016 at 1:04
  • 3 @passion That will stringify entries, ignore key, and check if Object has a property with that string. – Oriol Commented Sep 2, 2016 at 1:21
  • 3 @Phil Stackoverflow is the docs for many. – HosseyNJF Commented Jul 13, 2022 at 11:34
Add a comment  | 

9 Answers 9

Reset to default 494

You can access it via 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 hasOwnProperty method could be shadowed by something else.

Of course, the code above assumes that

  • The global Object has not been shadowed or redefined
  • The native Object.prototype.hasOwnProperty has not been redefined
  • No call own property has been added to Object.prototype.hasOwnProperty
  • The native Function.prototype.call has not been redefined

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 call would be

!!Object.getOwnPropertyDescriptor(obj, prop);

You can use Object.hasOwn(obj, prop) instead. hasOwn is intended to be a replacement for Object.hasOwnProperty.

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
}

It seems like this would also work:

key in entries

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

@Orial answer is correct

Use this:

Object.prototype.hasOwnProperty.call(object, "objectProperty");

To make code more elegant it can be in form a polyfill:

if (!Object.hasOwn) {
    Object.prototype.hasOwn = function (object, property) {
        return Object.prototype.hasOwnProperty.call(object, property);
    }
}

console.log(Object.hasOwn({test: 1}, 'test'), Object.hasOwn({tes: 1}, 'test'))

this is working for me so try with it

let  bug={
 name:"test"
   }
if (bug && typeof bug === 'object' && Object.prototype.hasOwnProperty.call(bug, name)) {

}

Object.hasOwn() is officially replacement for Object.prototype.hasOwnProperty().

if this objects has it's own property then it returns true.if property does not exist or inherited then it returns false.so hasOwn() static method and hasOwnProperty() method behaves in a same way.

const object1 = {
  prop: 'exists',
};

console.log(Object.hasOwn(object1, 'prop')); //true.

your code will be

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

I've ended up using this code:

const _ = require('lodash');
_.has(entries, key)

本文标签: javascriptObjecthasOwnProperty() yields the ESLint 39noprototypebuiltins39 error how to fixStack Overflow