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.

    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
    });

    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: T, orig: T[K], ...args: Parameters<T[K]>) => void)

      The method to replace it with

        • (this: T, orig: T[K], ...args: Parameters<T[K]>): void
        • Parameters

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

          Returns void

    Returns void

Generated using TypeDoc v0.23.24