Add `Object.hasOwn` to Section 3.7 (Object.prototype methods)
- Dominant language
- JavaScript
- Stars
- 148k
- Forks
- 26.6k
- PR merge metrics
- No merged PRs in 30d
Description
Hello, it seems that `Object.hasOwn` is now available in most major browsers (https://caniuse.com/mdn-javascript_builtins_object_hasown), so it might be worth adding it to section [3.7](https://github.com/airbnb/javascript#objects--prototype-builtins).
It seems to be recommended as a replacement for `Object.hasOwnProperty` (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwn), so the section could be changed to something like this:
- [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)`). In modern browsers `Object.hasOwn` can be used, which works for null objects and objects, where `hasOwnProperty` has been overridden.
```javascript
// bad
console.log(object.hasOwnProperty(key));
// good
console.log(Object.prototype.hasOwnProperty.call(object, key));
// better
const has = Object.prototype.hasOwnProperty; // cache the lookup once, in module scope.
console.log(has.call(object, key));
// best
console.log(object.hasOwn(key));
/* or */
import has from 'has'; // https://www.npmjs.com/package/has
console.log(has(object, key));
```
There is also a rule for this available in eslint 8.5+ (https://eslint.org/docs/latest/rules/prefer-object-has-own).
Cheers
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with section 3.7, “Object.prototype methods,” in the JavaScript style guide and review the linked MDN and ESLint documentation for Object.hasOwn and prefer-object-has-own. Update the section wording and examples to cover Object.hasOwn, then verify that the guidance accurately reflects the cited browser support and ESLint rule.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- eslint, javascript
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100