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

    • Rest ...argArray: any[]

    Returns F

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

    Since

    0.9.8

    Example

    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