bind source npm

_.bind(func, thisArg, [partials])

Creates a function that invokes func with the this binding of thisArg and prepends any additional _.bind arguments to those provided to the bound function.

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.

Arguments

  1. func (Function)

    The function to bind.

  2. thisArg (*)

    The this binding of func.

  3. [partials] (...*)

    The arguments to be partially applied.

Returns (Function)

Returns the new bound function.

Example

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

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

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

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