Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get most specific X such that y instanceof X is true? [duplicate]

Tags:

javascript

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.

like image 550
kjo Avatar asked Sep 01 '13 23:09

kjo


1 Answers

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)

like image 141
James Porter Avatar answered Oct 27 '22 01:10

James Porter