Skip to content

Commit

Permalink
[readme] Add Object.hasOwn to section 3.7
Browse files Browse the repository at this point in the history
  • Loading branch information
savalaram-redkar authored and ljharb committed Aug 15, 2023
1 parent cb19177 commit 46ae3e2
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ Other Style Guides
<a name="objects--prototype-builtins"></a>
- [3.7](#objects--prototype-builtins) Do not call `Object.prototype` methods directly, such as `hasOwnProperty`, `propertyIsEnumerable`, and `isPrototypeOf`. eslint: [`no-prototype-builtins`](https://eslint.org/docs/rules/no-prototype-builtins)
> Why? These methods may be shadowed by properties on the object in question - consider `{ hasOwnProperty: false }` - or, the object may be a null object (`Object.create(null)`).
> Why? These methods may be shadowed by properties on the object in question - consider `{ hasOwnProperty: false }` - or, the object may be a null object (`Object.create(null)`). In modern browsers that support ES2022, or with a polyfill such as <https://npmjs.com/object.hasown>, `Object.hasOwn` can also be used as an alternative to `Object.prototype.hasOwnProperty.call`.
```javascript
// bad
Expand All @@ -305,9 +305,13 @@ Other Style Guides
// good
console.log(Object.prototype.hasOwnProperty.call(object, key));

// best
// better
const has = Object.prototype.hasOwnProperty; // cache the lookup once, in module scope.
console.log(has.call(object, key));

// best
console.log(Object.hasOwn(object, key)); // only supported in browsers that support ES2022

/* or */
import has from 'has'; // https://www.npmjs.com/package/has
console.log(has(object, key));
Expand Down

0 comments on commit 46ae3e2

Please sign in to comment.