@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 whrn calling the provided fn instance, and all of the arguments passed to the new function will be passed along to the original provided instance.

      Type Parameters

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

      Parameters

      • fn: F

        The function instance to be called

      • thisArg: T

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

      Returns F

      The value returned by the original fn after executing with the provided thisArg.

      0.9.8

      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
    • Creates a new function that when called will set the value of thisArg as the this keyword value whrn calling the provided fn instance, and all of the arguments passed to the new function will be passed along to the original provided instance.

      Type Parameters

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

      Parameters

      • fn: F

        The function instance to be called

      • thisArg: T

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

      • ...argArray: TArgs

      Returns BoundFunction<F, TArgs>

      The value returned by the original fn after executing with the provided thisArg.

      0.9.8

      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
    • 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

        The function instance to be called.

      • thisArg: T

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

      • ...argArray: any[]

        Zero or more arguments to pre-bind to the function.

      Returns F

      The original function type with the bound this value and any provided arguments.

      0.9.8

      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"