258

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.

tog22
  • 486
  • 1
  • 4
  • 21
booYah
  • 2,589
  • 2
  • 10
  • 4
  • 21
    You should probably read the docs. There are examples of *correct* code ~ http://eslint.org/docs/rules/no-prototype-builtins – Phil Sep 02 '16 at 01:01
  • 1
    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 Sep 02 '16 at 01:04
  • 3
    @passion That will stringify `entries`, ignore `key`, and check if `Object` has a property with that string. – Oriol Sep 02 '16 at 01:21
  • 1
    @Phil Stackoverflow *is* the docs for many. – HosseyNJF Jul 13 '22 at 11:34

7 Answers7

428

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);
Oriol
  • 274,082
  • 63
  • 437
  • 513
  • 1
    You save my day, i have to refactor old code from my friend and have to replace so many line of code – Jamviet.com Jul 29 '22 at 19:22
  • 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()`. – limeandcoconut May 22 '23 at 20:39
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
}
Zameer Ansari
  • 28,977
  • 24
  • 140
  • 219
27

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

Noslac
  • 446
  • 4
  • 5
  • 1
    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 https://tomdn.com?object.hasOwn. – salsbury Aug 19 '22 at 15:20
  • I see that hasOwn() is supposed to work, but it doesn't seem to be supported in Node v18.10.0. – aikimcr Nov 12 '22 at 00:04
19

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?

Mike Mathew
  • 866
  • 1
  • 7
  • 12
  • 13
    `hasOwnProperty` checks if a string or symbol is an own property. `key in entries` checks if it's an own or inherited one. – Oriol Sep 03 '16 at 23:33
13

@Orial answer is correct

Use this:

Object.prototype.hasOwnProperty.call(object, "objectProperty");
Archil Labadze
  • 4,049
  • 4
  • 25
  • 42
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'))
onalbi
  • 2,609
  • 1
  • 24
  • 36
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)) {

}

Mohammed Al-Reai
  • 2,344
  • 14
  • 18