Given some object y
, how can I find out the most specific X
such that the expression
y instanceof X
evaluates to true
?
For example, the following two expressions both evaluate to true
[] instanceof Object
[] instanceof Array
...but Array
is more specific than Object
.
As per MDN:
The instanceof operator tests whether an object has in its prototype chain the prototype property of a constructor.
So Kolink is correct that checking y.constructor
will get you your answer. (Note that this assumes you or someone else hasn't mangled y.constructor
in the meantime, i.e. that y
's __proto__
(the beginning of its prototype chain) actually points to y.constructor.prototype
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With