Lodash _.bindKey() method
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.placeholderfor 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;
thisis the host object when invoked normally. - Property identity: understanding that
obj.mcan 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
_.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.placeholderto skip slots. - Returns: a new wrapper (same
lengthcaveat as_.bind—not trimmed like native bind).
Partial greeting on an object method
Lodash doc shape: method lives on object, bound wrapper fixes "hi" as the first argument.
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!" 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.
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!" Placeholder on bindKey
Lock punctuation, supply the greeting when the wrapper runs—same semantics as the official _, '!' example once placeholders resolve.
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!" 📋 _.bindKey vs _.bind
| Topic | _.bindKey | _.bind |
|---|---|---|
| What gets stored | object + property name | A specific function reference |
| Method replaced later | Wrapper calls the new implementation | Still invokes the original function |
| Typical use | Hot-swappable handlers, lazy definitions | Stable callbacks with fixed logic |
Native Function.prototype.bind also freezes the target function—compare with _.bind() on this site.
Pitfalls to avoid
Typos or deletes
Renaming or deleting object[key] breaks invocations—keep keys in sync with callers.
Lexical this
Arrow functions on object ignore the receiver Lodash passes; use ordinary functions when this must be object.
Prototype chain
Resolution follows Lodash internals—ensure you understand whether your method lives own or inherited if behavior surprises you.
❓ FAQ
Summary
- Purpose:
_.bindKey(object, key, ...partials)invokes the current method atkeywith boundthisand partial arguments. - Contrast:
_.bindcloses over a fixed function;bindKeyresolves at call time. - Next: Lodash _.curry(), _.bind(), or official Lodash docs for _.bindKey.
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.
6 people found this page helpful
