Function extend

  • Extend an object's method by running its output through a mutating callback every time it is called.

    The callback accepts the method's return value and should perform any mutations directly on this value. For this reason, this function will not be effective on methods which return scalar values (numbers, strings, booleans).

    Care should be taken to extend the correct object – in most cases, a class' prototype will be the desired target of extension, not the class itself.

    Type Parameters

    • T extends Record<string, any>

    • K extends string | number | symbol

    Parameters

    • object: string | T

      The object that owns the method

    • methods: K | K[]

      The name or names of the method(s) to extend

    • callback: ((this, val, ...args) => void)

      A callback which mutates the method's output

        • (this, val, ...args): void
        • Parameters

          Returns void

    Returns void

    Example

    Example usage of extending one method.

    extend(Discussion.prototype, 'badges', function(badges) {
    // do something with `badges`
    });

    Example

    Example usage of extending multiple methods.

    extend(IndexPage.prototype, ['oncreate', 'onupdate'], function(vnode) {
    // something that needs to be run on creation and update
    });

Generated using TypeDoc v0.24.8