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/undefinedare never instances: returnfalse. - Objects and functions are checked by walking their prototype chain.
- If
Constructoris not a function, throw aTypeError. - You do not need to support
Symbol.hasInstanceor 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
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 10. ⌘Enter runs, ⌘⇧Enter submits.