JS Built-ins
SymbolhasInstanceadvanced

Symbol.hasInstance β€” custom instanceof

πŸ“– Usage Example
class EvenNumber {
  static [Symbol.hasInstance](n: unknown) {
    return typeof n === 'number' && n % 2 === 0
  }
}
2 instanceof EvenNumber   // β†’ true

Define static [Symbol.hasInstance] on a class to override how instanceof checks membership.

Custom instanceof with Symbol.hasInstance

A class can define static [Symbol.hasInstance](instance) to customise instanceof.

Challenge: Create a class where all arrays are "instances".

πŸ’‘ Hints (1)
  • β€’ Define a static [Symbol.hasInstance] method on your class.