JavaScript changes once again!
(TL;DR: Use Object.hasOwn
now.)
The long standing highest-voted answer recommends using hasOwnProperty
. This was the best answer at the time, but it does have a number of tricky bits that can trip you up. For example, if the variable can be null
, this will cause a TypeError when you attempt to call hasOwnProperty
. Less commonly (but a symptom of the same problem), if an object could potentially have a key named "hasOwnProperty," that would shadow the function you wanted. (This could be a problem if, for example, you are parsing user-provided input.)
Because of all that, the previously correct way to call the function is this mess:
Object.prototype.hasOwnProperty.call(thisSession, "merchant_id");
In updated browsers and recent versions of node there is a new alternative that addresses these problems. Object
now has a static method named hasOwn
that addresses these pitfalls.
Object.hasOwn(thisSession, "merchant_id")
Ahh, much better. :)
If you are targeting browsers or node versions new enough to have hasOwn
, you should stop using hasOwnProperty
entirely. It is obsolete.
<?php echo json_encode($_POST); ?>
?console.log(thisSession);
?!("merchant_id" in thisSession)==0
where you can simply use"merchant_id" in thisSession
?