@nevware21/ts-utils
    Preparing search index...

    Function fnBind

    Creates a new function that when called will set the value of thisArg as the this keyword value when calling the provided fn instance, and all of the arguments passed to the new function will be passed along to the original provided instance (after any argArray arguments which are bound to it).

    fnBind is the intrinsic Function.prototype.bind bound to the intrinsic Function.prototype.call, so fnBind(fn, thisArg, ...argArray) is fn.bind(thisArg, ...argArray) without any wrapper function, and it doesn't depend on the bind, call or apply properties of fn (which may have been overridden). On a runtime without the native Function.prototype.bind (eg. ES3) it is a polyfill which returns a function that calls fn with thisArg and the bound arguments followed by its own arguments, using the intrinsic Function.prototype.apply (unlike a native bound function it can't be called with new, and it doesn't have the name and length of fn). Which one is used is resolved once, when the module is loaded.

    0.9.8

    The function instance to be called

    The value to be used as the this when calling the fn

    Zero or more arguments to pre-bind to the function

    The new (bound) function, which returns the value returned by the original fn after executing with the provided thisArg.

    const module1 = {
    x: 21,
    getX() {
    return this.x;
    },
    };

    // The 'this' parameter of 'getX' is bound to 'module'.
    console.log(module1.getX()); // 21

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

    module2.getX = fnBind(module1.getX, module2);
    module2.getX(); // 42

    // It can also be used to proxy to the original function from the new one
    module2.getX = fnBind(module1.getX, module1);
    module2.getX(); // 21

    // A less-specific `Function`-typed call site, where the concrete parameter list is not available
    const host = {
    prefix: "Hello",
    log(value: string) {
    return this.prefix + " " + value;
    }
    };

    const dynamicLog: Function = host.log;
    const bound = fnBind(dynamicLog, host);

    bound("friend"); // "Hello friend"
    • Binds the thisArg to the fn, the new function is called with its own arguments

      Type Parameters

      • F extends (...args: any[]) => any
      • T

      Parameters

      • fn: F
      • thisArg: T

      Returns F

    • Binds the thisArg and the argArray arguments to the fn, the new function is called with the bound arguments followed by its own arguments

      Type Parameters

      • F extends (...args: any[]) => any
      • T
      • TArgs extends any[]

      Parameters

      • fn: F
      • thisArg: T
      • ...argArray: TArgs

      Returns BoundFunction<F, TArgs>

    • Fallback overload for less-specific Function-typed call sites where the concrete parameter list is not available, such as dynamic proxy wiring.

      Type Parameters

      • F extends Function
      • T

      Parameters

      • fn: F
      • thisArg: T
      • ...argArray: any[]

      Returns F