Sub topic 16
Type Checks with instanceof and isPrototypeOf
This chapter assumes you already know how the prototype chain is walked, how new links an instance to Constructor.prototype, and how class and extends set up prototypes. Both operators covered here are questions about the prototype chain. Neither one checks how an object was actually built.
What instanceof Actually Checks
obj instanceof Ctor asks one question: does Ctor.prototype appear anywhere in the prototype chain of obj? It never looks at the constructor that created the object.
class Animal {}
class Dog extends Animal {}
const rex = new Dog();
console.log(rex instanceof Dog); // true
console.log(rex instanceof Animal); // true
console.log(rex instanceof Object); // true
console.log(Animal.prototype.isPrototypeOf(rex)); // true
console.log(Dog.prototype.isPrototypeOf(Animal.prototype)); // falseThe algorithm can be written by hand in a few lines:
function isInstance(obj, Ctor) {
if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
return false;
}
let proto = Object.getPrototypeOf(obj);
while (proto !== null) {
if (proto === Ctor.prototype) return true;
proto = Object.getPrototypeOf(proto);
}
return false;
}
class Animal {}
class Dog extends Animal {}
console.log(isInstance(new Dog(), Animal)); // true
console.log(isInstance(42, Number)); // falseisPrototypeOf: Checking Without a Constructor
proto.isPrototypeOf(obj) runs the same walk, but you pass in the prototype object itself. This makes it the right tool for objects built with Object.create, which often have no constructor at all.
const animal = { eats: true };
const rabbit = Object.create(animal);
console.log(animal.isPrototypeOf(rabbit)); // true
console.log(Object.prototype.isPrototypeOf(rabbit)); // trueLimitations and Gotchas
function Person() {}
const p = new Person();
Person.prototype = {}; // replace the prototype object
console.log(p instanceof Person); // false: the chain still points at the old object
const dict = Object.create(null);
console.log(dict instanceof Object); // false: no Object.prototype in the chain
console.log('hi' instanceof String); // false: primitives have no chain to walk
console.log(Array.isArray([])); // true: works across realms| Situation | Result | Better option |
|---|---|---|
| `Ctor.prototype` reassigned after creation | `false` for old instances | Avoid reassigning prototypes |
| Null-prototype object | `false` for `Object` | `Object.getPrototypeOf(x) === null` |
| Primitive value | always `false` | `typeof` |
| Array from another iframe or realm | `false` for `Array` | `Array.isArray` |
Duck Typing as an Alternative
Often you care only that an object has a certain method, not where that method came from. In that case, typeof obj.speak === 'function' is more flexible than an instanceof check and works with mixins and plain objects.
Exercise
Given const base = {}; const mid = Object.create(base); const leaf = Object.create(mid);, what do base.isPrototypeOf(leaf), leaf.isPrototypeOf(base), and leaf instanceof Object return?
Show answer
true, false, and true. base is two steps up leaf's chain. leaf is not in base's chain. base was created by an object literal, so its own prototype is Object.prototype, and that puts Object.prototype in leaf's chain.
Exercise
Why does Object.create(null).isPrototypeOf throw a TypeError when you try to call it, and how can you check such an object anyway?
Show answer
A null-prototype object inherits nothing, so it has no isPrototypeOf method. Call the method with an explicit receiver instead, for example Object.prototype.isPrototypeOf.call(proto, obj), or compare against Object.getPrototypeOf(obj) directly.