question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Docs: Explain why `{}.hasOwnProperty.call` is recommended

See original GitHub issue

A coworker just cleaned up a bunch of lint errors in the project, and left this headscratcher in the codebase:

for(let key in obj) {
      if ({}.hasOwnProperty.call(obj, key)) {
          // ...
      }
}

When asked what was going on here and why not just use obj.hasOwnProperty(key) or even Object.prototype.hasOwnProperty.call(obj, key), he pointed to the docs for guard-for-in, which, sure enough, recommends this pattern as a solution.

I dug through the history of the docs page, assuming to maybe find it as part of a commit that expanded the detection capabilities of the rule (and the docs would demonstrate just how crazy you could write the call and still satisfy the rule), but it turns out it is in fact recommended as a “best practice”: https://github.com/eslint/eslint/commit/551335eedf62261299af7aafdfc896b3aef8de67

I’d recommend the docs page for this rule be amended to explain exactly why this sort of gymnastics is demonstrated over a simpler approach. Is it meant to avoid an edge case where hasOwnProperty is defined and thus overridden higher in the prototype chain?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
ljharbcommented, Sep 8, 2016

(fwiw, this is why the Airbnb style guide and virtually every “best practice” i’ve seen discourages use of for..in - Object.keys(obj).forEach does not have this problem)

2reactions
not-an-aardvarkcommented, Sep 7, 2016

To answer your question, Object.prototype.hasOwnProperty.call is generally used for two reasons:

  1. hasOwnProperty is defined on the object as something else, as you mentioned:

    var someObject = {foo: 'bar', hasOwnProperty: 5};
    someObject.hasOwnProperty('foo') // throws a TypeError
    
  2. The object in question is being used as a map and doesn’t inherit from Object.prototype, so it doesn’t have hasOwnProperty:

    var someObject = Object.create(null);
    someObject.hasOwnProperty('foo'); // throws a TypeError
    

See also: no-prototype-builtins

Read more comments on GitHub >

github_iconTop Results From Across the Web

Object.prototype.hasOwnProperty() - JavaScript | MDN
The hasOwnProperty() method returns a boolean indicating whether the object has the specified property as its own property (as opposed to ...
Read more >
Why use Object.prototype.hasOwnProperty.call(myObj, prop ...
The in operator will inspect down through the prototype chain too. This means that instance properties will return true when passed to  ......
Read more >
What's the deal with Object.prototype.hasOwnProperty.call()?
The answer is via call and apply method available on Function.prototype. In JavaScript, every function we create inherits from Function.
Read more >
Object.prototype.hasOwnProperty()
The hasOwnProperty() method returns a boolean indicating whether the object has the specified property as its own property (as opposed to ...
Read more >
The right way to check if an object has a property in JavaScript
So you think you could use it this way: myObject.hasOwnProperty('id') but guess what ? It is recommended to use: Object.prototype.hasOwnProperty.call ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found