January 2020
Intermediate to advanced
548 pages
13h 36m
English
The instanceof operator in JavaScript allows you to detect whether an object is an instance of a constructor:
const component = new Component();component instanceof Component;
This operation will climb the [[Prototype]] chain of its left-side operand looking for a specific constructor function. It will then check whether this constructor is equal to the right-side operand.
Since it climbs the [[Prototype]] chain, it can work safely with multiple inheritances:
class Super {}class Child extends Super {}new Super() instanceof Super; // => truenew Child() instanceof Child; // => truenew Child() instanceof Super; // => true
If the right-side operand is not a function (that is, is not callable as a constructor), then
Read now
Unlock full access