You can access it via Object.prototype:
Object.prototype.hasOwnProperty.call(obj, prop);
That should be safer, because
- Not all objects inherit from
Object.prototype - Even for objects which inherit from
Object.prototype, thehasOwnPropertymethod could be shadowed by something else.
Of course, the code above assumes that
- The global
Objecthas not been shadowed or redefined - The native
Object.prototype.hasOwnPropertyhas not been redefined - No
callown property has been added toObject.prototype.hasOwnProperty - The native
Function.prototype.callhas not been redefined
If any of these does not hold, attempting to code in a safer way, you could have broken your code!
Another approach which does not need call would be
!!Object.getOwnPropertyDescriptor(obj, prop);