4.17.11
3.10.1
2.4.2
1.3.1

_.bindKey(object, key, [partials])

Creates a function that invokes the method at object[key] with partials prepended to the arguments it receives.

This method differs from _.bind by allowing bound functions to reference methods that may be redefined or don't yet exist. See Peter Michaux's article for more details.

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

Since

0.10.0

Arguments

argument
object
key
[partials]
type
Object
string
...*
description
The object to invoke the method on.
The key of the method.
The arguments to be partially applied.

Returns

(array)

Example

var object = {
  user: "fred",
  greet: function (
    greeting,
    punctuation
  ) {
    return (
      greeting +
      " " +
      this.user +
      punctuation
    );
  },
};

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

object.greet = function (
  greeting,
  punctuation
) {
  return (
    greeting +
    "ya " +
    this.user +
    punctuation
  );
};

bound("!");
// => 'hiya fred!'

// Bound with placeholders.
var bound = _.bindKey(
  object,
  "greet",
  _,
  "!"
);
bound("hi");
// => 'hiya fred!'
var object = {
  user: "fred",
  greet: function (greeting, punctuation) {
    return greeting + " " + this.user + punctuation;
  },
};

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

object.greet = function (greeting, punctuation) {
  return greeting + "ya " + this.user + punctuation;
};

bound("!");
// => 'hiya fred!'

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