Object.hasOwnProperty() yields the ESLint 'no-prototype-builtins' error: how to fix?
Asked Answered
L

8

291

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.

Lulu answered 2/9, 2016 at 0:57 Comment(4)
You should probably read the docs. There are examples of correct code ~ eslint.org/docs/rules/no-prototype-builtinsMonarski
The code is working fine. This is a linting error. I just want to modify the syntax so that the linting rule is satisfied.Lulu
@passion That will stringify entries, ignore key, and check if Object has a property with that string.Democratic
@Monarski Stackoverflow is the docs for many.Ootid
D
472

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);
Democratic answered 2/9, 2016 at 1:17 Comment(4)
You save my day, i have to refactor old code from my friend and have to replace so many line of codeViscardi
If you're going to be cautious enough to use Object.getOwnPropertyDescriptor then it would definitely be preferable to use an explicit cast with Boolean().Boycie
@Boycie Using !! is more cautious than Boolean() since the former cannot be overwrittenLuana
I suppose that's true, but I'm not concerned with the fact that Boolean could be theoretically overridden. The much greater trade off imo is the loss of specificity, and maintainability. I'm firmly in the camp that the things like !! are harder to read, less specific, and easier to accidentally mess up during a later edit than doing an explicit cast.Boycie
T
54

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
}
Tripitaka answered 3/7, 2018 at 6:53 Comment(1)
The last example(propertyIsEnumerable) works for me thanksSplit
D
46

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

Darnall answered 7/7, 2022 at 9:43 Comment(2)
This is the way to go in 2022. It has good browser support, is shorter and easier to remember than the accepted answer, and solves the problem ESLint is complaining about. MDN says, Note: Object.hasOwn() is intended as a replacement for Object.hasOwnProperty(). See tomdn.com?object.hasOwn.Perez
I see that hasOwn() is supposed to work, but it doesn't seem to be supported in Node v18.10.0.Schargel
S
20

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?

Softball answered 2/9, 2016 at 19:15 Comment(1)
hasOwnProperty checks if a string or symbol is an own property. key in entries checks if it's an own or inherited one.Democratic
G
14

@Orial answer is correct

Use this:

Object.prototype.hasOwnProperty.call(object, "objectProperty");
Gallous answered 9/11, 2021 at 19:16 Comment(4)
oof. that is not elegant....Seat
@Seat write elegant please )Gallous
No Im not saying the post is inelegant, just the API is inelegant lolSeat
@Seat ah got it, yes agree, it's bit uncomfortable )Gallous
K
2

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'))
Kizzie answered 11/4, 2023 at 18:29 Comment(0)
U
0

this is working for me so try with it

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

}

Unsuitable answered 3/10, 2021 at 9:18 Comment(1)
ok, I do that thank you for telling meUnsuitable
D
0

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];
}
Duleba answered 21/1 at 12:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.