Function createFnDeferredProxy

  • Create a deferred proxy function which will call the named function of the result of the hostFn, this enables creating bound functions which when called call the proxy the function to a different host (this) instance.

    This is different from fnBind which is provided with the concrete function and this instances, while the proxy will lazily obtain the this and the function is obtained by looking up the named function from the returned host (this) instance.

    Type Parameters

    • H
    • F extends ((...args) => any)

    Parameters

    • hostFn: (() => H)

      A function to get the current host and thisArg that will be called

        • (): H
        • Returns H

    • funcName: TypeFuncNames<H>

      The name of the function to call on the host

    Returns F

    The result of calling the function with the specified this value and arguments.

    Since

    0.9.8

    Example

    const module1 = {
    prefix: "Hello",
    x: 21,
    getX() {
    return this.x;
    },
    log(value: string) {
    return this.prefix + " " + value + " : " + this.x
    }
    };

    // The 'this' parameter of 'getX' is bound to 'module'.
    module1.getX(); // 21
    module1.log("Darkness"); // Hello Darkness : 21

    // Create a new function 'boundGetX' with the 'this' parameter bound to 'module'.
    let module2 = {
    prefix: "my",
    x: 42
    };

    let getHost = () => {
    return module1;
    };

    let deferredFn = createFnDeferredProxy(getHost, "getX");

    deferredFn(); // 21

    module2.defX = deferredFn;

    module2.defX(); // 21