Function iteratePrototypeChain

  • Iterate the prototype chain of a given object.

    Parameters

    • object: Record<string, any>

      The object whose prototype chain is to be iterated over.

    Returns Generator<Record<string, any>>

    A generator that yields each prototype in the chain.

    Remarks

    This function uses the Reflect.getPrototypeOf method to traverse the prototype chain. It also uses the isPrototype and isConstructor helper functions to determine whether the given object is a prototype or a constructor function.

    Throws

    If the provided object is not an object or a function, a TypeError will be thrown.

    Example

    class A {}
    class B extends A {}
    class C extends B {}
    const instance = new C()
    iteratePrototypeChain(C)
    //=> [ C, B, A, Function.prototype, Object.prototype]
    iteratePrototypeChain(C.prototype)
    //=> [C.prototype, B.prototype, A.prototype, Object.prototype]
    iteratePrototypeChain(instance)
    //=> [instance, C.prototype, B.prototype, A.prototype, Object.prototype]

Generated using TypeDoc