Skip to content

Commit 46ae3e2

Browse files
savalaram-redkarljharb
authored andcommitted
[readme] Add Object.hasOwn to section 3.7
1 parent cb19177 commit 46ae3e2

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ Other Style Guides
296296
<a name="objects--prototype-builtins"></a>
297297
- [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)
298298
299-
> 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)`).
299+
> 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`.
300300
301301
```javascript
302302
// bad
@@ -305,9 +305,13 @@ Other Style Guides
305305
// good
306306
console.log(Object.prototype.hasOwnProperty.call(object, key));
307307

308-
// best
308+
// better
309309
const has = Object.prototype.hasOwnProperty; // cache the lookup once, in module scope.
310310
console.log(has.call(object, key));
311+
312+
// best
313+
console.log(Object.hasOwn(object, key)); // only supported in browsers that support ES2022
314+
311315
/* or */
312316
import has from 'has'; // https://www.npmjs.com/package/has
313317
console.log(has(object, key));

0 commit comments

Comments
 (0)