Javascript object.hasOwnProperty() vs Reflect.has()
One major difference is that Reflect.has
will check whether any of the object's internal prototypes have the key, whereas hasOwnProperty
only checks whether the object itself has the key:
const proto = { foo: 'foo' };
const obj = Object.create(proto);
console.log(Reflect.has(obj, 'foo'));
console.log(obj.hasOwnProperty('foo'));
If you want to check whether the object itself has the property, and isn't an inherited property, you should definitely use hasOwnProperty
.
Another important difference is that Reflect.has
requires ES6 support, whereas hasOwnProperty
has existed since ES3, so if you use Reflect.has
and require support for older browsers, make sure to include a polyfill.
Reflect api is a part of ECMAScript 2015 whether hasOwnProperty is available in current standard. Their behavior may differ too. So if they function identically it only depends on whether you want to support older browsers or not.
https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Reflect
https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
You can polyfill it with seral libraries that can be found on github/npmjs like: https://www.npmjs.com/package/reflect-metadata
https://www.npmjs.com/package/harmony-reflect