Description
The instanceof
operator tests the presence of constructor.prototype
in object
's prototype chain. This usually means object
was constructed with constructor
.
// defining constructors function C() {} function D() {} const o = new C(); // true, because: Object.getPrototypeOf(o) === C.prototype o instanceof C // false, because D.prototype is nowhere in o's prototype chain o instanceof D o instanceof Object // true, because: C.prototype instanceof Object // true // Re-assign `constructor.prototype`: you should // rarely do this in practice. C.prototype = {}; const o2 = new C(); o2 instanceof C // true // false, because C.prototype is nowhere in // o's prototype chain anymore o instanceof C D.prototype = new C() // add C to [[Prototype]] linkage of D const o3 = new D(); o3 instanceof D // true o3 instanceof C // true since C.prototype is now in o3's prototype chain
Note that the value of an instanceof
test can change if constructor.prototype
is re-assigned after creating the object (which is usually discouraged). It can also be changed by changing object
's prototype using Object.setPrototypeOf
.
Classes behave in the same way, because classes also have the prototype
property.
class A {} class B extends A {} const o1 = new A(); // true, because Object.getPrototypeOf(o1) === A.prototype o1 instanceof A // false, because B.prototype is nowhere in o1's prototype chain o1 instanceof B const o2 = new B(); // true, because Object.getPrototypeOf(Object.getPrototypeOf(o2)) === A.prototype o2 instanceof A // true, because Object.getPrototypeOf(o2) === B.prototype o2 instanceof B
If constructor
has a Symbol.hasInstance
method, the method will be called in priority, with object
as its only argument and constructor
as this
.
// This class allows plain objects to be disguised as this class's instance, // as long as the object has a particular flag as its property. class Forgeable { static isInstanceFlag = Symbol("isInstanceFlag"); static [Symbol.hasInstance](obj) { return Forgeable.isInstanceFlag in obj; } } const obj = { [Forgeable.isInstanceFlag]: true }; console.log(obj instanceof Forgeable); // true
instanceof
and multiple contexts (e.g. frames or windows)
Different scopes have different execution environments. This means that they have
different built-ins (different global object, different constructors, etc.). This may
result in unexpected results. For instance,
[] instanceof window.frames[0].Array
will return false
,
because Array.prototype !== window.frames[0].Array.prototype
and arrays
inherit from the former.
This may not make sense at first, but for scripts dealing with multiple frames or
windows, and passing objects from one context to another via functions, this will be a
valid and strong issue. For instance, you can securely check if a given object is, in
fact, an Array using Array.isArray(myObj)
For example, checking if a Node is a SVGElement in a different context, you can
use myNode instanceof myNode.ownerDocument.defaultView.SVGElement
.