Skip to curriculum
Jump to the editor

9.6. Implement instanceof

Medium
Topics

This question is in

9. Prototypes and classes
15–25 min

value instanceof Constructor looks like it asks "did Constructor make this value?", but it asks something else: is Constructor.prototype anywhere on the value's chain of prototypes? Rebuilding it shows exactly what it checks, why Object.create(Animal.prototype) passes it, and why "text" instanceof String is false.

Write isInstanceOf(value, Constructor) in instanceof.js. It returns the same result as value instanceof Constructor, and throws a TypeError in the same cases, without using instanceof itself.

Signature

JAVASCRIPT
function isInstanceOf(value, Constructor) // returns true or false
  • value: anything.
  • Constructor: should be a function with a prototype object.

Requirements

  1. Returns true when Constructor.prototype is the prototype of value, or the prototype of that prototype, and so on up the chain; otherwise false. Always a boolean.
  2. Primitives (strings, numbers, booleans, symbols, bigints), null and undefined give false, even for their own wrapper ("text" with String). Wrapper objects like new String("text") are instances.
  3. Functions and classes on the left are objects too: an arrow function is an instance of Function and Object.
  4. An object whose chain does not reach Object.prototype, like Object.create(null), is not an instance of Object.
  5. Only the chain counts, not how the object was made: Object.create(Animal.prototype) is an instance of Animal.
  6. The chain and Constructor.prototype are read at the time of the call: after Object.setPrototypeOf or after Constructor.prototype is replaced, the answer follows.
  7. The real prototype is used: an own property named __proto__ does not change the answer.
  8. Throws a TypeError when Constructor is not a function ({}, null, a string, an object with a prototype property), checked first, even when value is a primitive.
  9. Throws a TypeError when value is an object and Constructor.prototype is not an object (an arrow function, or a function whose prototype was set to 5). A primitive value returns false before prototype is read.
  10. Works on a chain 100,000 objects long.
  11. The function does not use instanceof, isPrototypeOf or Symbol.hasInstance.

Examples

JAVASCRIPT
class Animal {}
class Dog extends Animal {}

isInstanceOf(new Dog(), Animal);                 // true
isInstanceOf(new Dog(), Array);                  // false
isInstanceOf(Object.create(Animal.prototype), Animal); // true
JAVASCRIPT
isInstanceOf("text", String);                    // false
isInstanceOf(new String("text"), String);        // true
isInstanceOf(Object.create(null), Object);       // false
isInstanceOf(() => {}, Function);                // true
JAVASCRIPT
isInstanceOf({}, {});                            // TypeError: not a function
isInstanceOf({}, () => {});                      // TypeError: no prototype object
isInstanceOf(1, () => {});                       // false

Environment

  • Plain JavaScript in a browser page; the tests call the global isInstanceOf from instanceof.js and compare it with the real instanceof.
  • Not allowed inside the function: instanceof, Object.prototype.isPrototypeOf, Symbol.hasInstance.
  • Out of scope: a custom static [Symbol.hasInstance] method on the constructor, and bound functions (the built-in uses their target function). The tests use neither.

Hints

  1. Which function returns an object's real prototype, whatever own properties it has?
  2. typeof gives "object" for one value that has no prototype chain at all. Which one?
  3. Recursion is natural for walking a chain. What happens to it at 100,000 levels?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…