Lodash _.bindKey() method

Beginner
⏱️ 7 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • How _.bindKey(object, key, ...partials) wraps a method slot instead of a bare function reference.
  • Why reassigned methods change behavior without rebuilding the wrapper.
  • Using bindKey.placeholder for deferred arguments.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Read _.bind() first for partial application and placeholders; skim the Function hub for context.

  • Object methods: functions stored as own properties; this is the host object when invoked normally.
  • Property identity: understanding that obj.m can point to a different function after assignment.

Overview

_.bindKey(object, key, ...partials) returns a wrapper that invokes whatever function currently lives at object[key], binding this to object and prepending partial arguments—matching Lodash’s documented greeting examples and the lazy-definition pattern described in Peter Michaux’s article linked from the official docs.

Late lookup

Swap implementations on object; the wrapper picks up the latest method automatically.

Placeholders

Defer selected parameters exactly like _.bind via bindKey.placeholder.

Tree-shakeable

Import lodash/bindKey when bundlers should exclude unrelated helpers.

Syntax

javascript
_.bindKey(object, key, ...partials)
  • object: receiver whose method runs with this === object.
  • key: string name of the method (Lodash passes this through its wrap pipeline).
  • partials: optional leading arguments; use bindKey.placeholder to skip slots.
  • Returns: a new wrapper (same length caveat as _.bind—not trimmed like native bind).
1

Partial greeting on an object method

Lodash doc shape: method lives on object, bound wrapper fixes "hi" as the first argument.

javascript
import bindKey from "lodash/bindKey";

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

const bound = bindKey(object, "greet", "hi");

bound("!");
// => "hi fred!"
Try it Yourself
2

Replacing object.greet updates behavior

Same wrapper as example 1—after you assign a new function to object.greet, the next call runs the new body without recreating bound.

javascript
import bindKey from "lodash/bindKey";

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

const bound = bindKey(object, "greet", "hi");

bound("!");
// => "hi fred!"

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

bound("!");
// => "hiya fred!"
Try it Yourself
3

Placeholder on bindKey

Lock punctuation, supply the greeting when the wrapper runs—same semantics as the official _, '!' example once placeholders resolve.

javascript
import bindKey from "lodash/bindKey";

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

const bound = bindKey(object, "greet", bindKey.placeholder, "!");

bound("hi");
// => "hiya fred!"
Try it Yourself

📋 _.bindKey vs _.bind

Topic_.bindKey_.bind
What gets storedobject + property nameA specific function reference
Method replaced laterWrapper calls the new implementationStill invokes the original function
Typical useHot-swappable handlers, lazy definitionsStable callbacks with fixed logic

Native Function.prototype.bind also freezes the target function—compare with _.bind() on this site.

Pitfalls to avoid

Wrong key

Typos or deletes

Renaming or deleting object[key] breaks invocations—keep keys in sync with callers.

Arrow methods

Lexical this

Arrow functions on object ignore the receiver Lodash passes; use ordinary functions when this must be object.

Inherited props

Prototype chain

Resolution follows Lodash internals—ensure you understand whether your method lives own or inherited if behavior surprises you.

❓ FAQ

bind captures a specific function value at creation time. bindKey stores the object and property name, so each invocation looks up the current method—useful for hot-swapping implementations or methods defined later.
Resolution follows Lodash’s internal wrap path; calling the wrapper when the key holds a non-function typically throws when invocation is attempted—ensure the property exists or guard callers.
Yes—use bindKey.placeholder (or the shared Lodash placeholder in monolithic builds) to defer specific argument positions.
No—it delegates to whatever function lives at key on each call, with this set to object.
Use import bindKey from "lodash/bindKey"; bindKey.placeholder is attached for modular builds.

Summary

Did you know?

bindKey sets Lodash’s WRAP_BIND_KEY_FLAG so createWrap resolves object[key] when the wrapper runs—not when you create it—which is why reassigning object.greet changes what the bound function calls.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful