Function bind

  • Creates a function that invokes func with the this binding of thisArg and partials prepended to the arguments it receives.

    The bind.placeholder value, which defaults to _ in monolithic builds, may be used as a placeholder for partially applied arguments.

    Note: Unlike native Function#bind, this method doesn't set the "length" property of bound functions.

    Type Parameters

    • T

    Parameters

    • func: T

      The function to bind.

    • Optional thisArg: any

      The this binding of func.

      Optional
    • Rest ...partials: any[]

      The arguments to be partially applied.

      Rest

    Returns T

    Returns the new bound function.

    Since

    5.3.0

    Example

    function greet(greeting, punctuation) {
    return greeting + ' ' + this.user + punctuation;
    }

    var object = { 'user': 'fred' };

    var bound = bind(greet, object, 'hi');
    bound('!');
    // => 'hi fred!'

    // Bound with placeholders.
    var bound = bind(greet, object, _, '!');
    bound('hi');
    // => 'hi fred!'