Today, it is very common to write code like this:
const hasOwnProperty = Object.prototype.hasOwnProperty;
if (hasOwnProperty.call(object, 'foo')) {
// `object` has property `foo`.
}
Or to use libraries that expose a simple version of Object.prototype.hasOwnProperty
, such as has or lodash.has.
With the Object.hasOwn
proposal, we can simply write:
if (Object.hasOwn(object, 'foo')) {
// `object` has property `foo`.
}
Object.hasOwn
is already available in V8 v9.3 behind the --harmony-object-has-own
flag, and we’ll be rolling it out in Chrome soon.
Object.hasOwn
support #
- Chrome: 支持
- Firefox: 支持
- Safari: 支持
- Node.js: 不支持
- Babel: 支持