Function override

  • Override an object's method by replacing it with a new function, so that the new function will be run every time the object's method is called.

    The replacement function accepts the original method as its first argument, which is like a call to super. Any arguments passed to the original method are also passed to the replacement.

    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<any, any>

    • K extends string | number | symbol

    Parameters

    • object: T

      The object that owns the method

    • methods: K | K[]

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

    • newMethod: ((this, orig, ...args) => void)

      The method to replace it with

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

          • this: T
          • orig: T[K]
          • Rest ...args: Parameters<T[K]>

          Returns void

    Returns void

    Example

    Example usage of overriding one method.

    override(Discussion.prototype, 'badges', function(original) {
    const badges = original();
    // do something with badges
    return badges;
    });

    Example

    Example usage of overriding multiple methods.

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

Generated using TypeDoc v0.24.8