CodePark/ problems / instanceOf
START PRACTICINGPRACTICE
Coding problem · JavaScript utilities › this, call/apply/bind and prototypes

instanceOf

easyabout 10 minutes in an interview

Implement instanceOf(value, Ctor). It returns true when Ctor.prototype appears anywhere in the prototype chain of value, and false otherwise.

The prototype chain is the list of objects you reach by asking an object for its prototype, then asking that prototype for its own, until you reach null.

  • Don't use the instanceof operator or isPrototypeOf.
  • Primitives (42, "hi", true, null, undefined) always give false.
  • Ignore any custom Symbol.hasInstance method on Ctor. Only the chain counts.
  • If Ctor is not a function, throw a TypeError.
Input: class A {}; class B extends A {}; instanceOf(new B(), A) Output: true
Input: instanceOf([], Map) Output: false
Input: instanceOf(Object.create(null), Object) Output: falsethe chain is empty, so Object.prototype is never reached