Skip to content
Onsite

instanceof Implementation

MediumP3Coding

Implement myInstanceof(value, Constructor), which behaves like value instanceof Constructor without using the instanceof operator.

It returns true if Constructor.prototype appears anywhere in the prototype chain of value, and false otherwise.

  • Primitives (numbers, strings, booleans, symbols, bigint) and null/undefined are never instances: return false.
  • Objects and functions are checked by walking their prototype chain.
  • If Constructor is not a function, throw a TypeError.
  • You do not need to support Symbol.hasInstance or bound functions.

Example 1

Input
class A {} class B extends A {} myInstanceof(new B(), A)
Output
true

Constraints

  • Prototype chains are at most 10^3 links long.

Hints

Run checks the 3 visible tests, Submit all 10.