Lodash _.bind() method
What you’ll learn
- How
_.bind(func, thisArg, ...partials)fixesthisand prepends bound arguments. - Using
_.bind.placeholderto leave holes filled on later calls. - Trade-offs versus native
bind(especially functionlength). - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Optional reads Function hub, _.before(), and _.ary(); knowing how this behaves in plain functions helps.
thisbinding: methods expect a receiver object; detached references lose it unless wrapped.- Partial application: fixing the first arguments while leaving later ones open.
Overview
_.bind(func, thisArg, ...partials) returns a wrapper that calls func with this set to thisArg and any supplied partial arguments stitched before runtime parameters. Placeholders mark positions deferred until the wrapper runs—mirroring the official Lodash documentation greeting example.
Stable this
Extract methods as callbacks without losing the intended receiver.
Placeholders
Mix fixed and late-bound arguments in one wrapper.
Tree-shakeable
Import lodash/bind when the full bundle is unnecessary.
Syntax
_.bind(func, thisArg, ...partials) - func: the function to wrap; must be callable or Lodash throws.
- thisArg: value passed as
thiswhenfuncruns. - partials: optional leading arguments; include
_.bind.placeholderto skip slots filled by later calls. - Returns: a new bound function (Lodash does not adjust its
lengthlike nativebind).
this plus a fixed greeting
Classic doc pattern: partially apply the greeting while this.user supplies the name fragment.
import bind from "lodash/bind";
function greet(greeting, punctuation) {
return greeting + " " + this.user + punctuation;
}
const object = { user: "fred" };
const bound = bind(greet, object, "hi");
bound("!");
// => "hi fred!" Bound with placeholders
Reserve the first argument for the eventual call while locking punctuation ahead of time.
import bind from "lodash/bind";
function greet(greeting, punctuation) {
return greeting + " " + this.user + punctuation;
}
const object = { user: "fred" };
const bound = bind(greet, object, bind.placeholder, "!");
bound("hi");
// => "hi fred!" Method as callback
Borrow an object method for Array.prototype.map while keeping the correct this for property reads.
import bind from "lodash/bind";
const counter = {
prefix: "Item ",
label(n) {
return this.prefix + n;
}
};
const labels = [1, 2].map(bind(counter.label, counter));
// => ["Item 1", "Item 2"] 📋 _.bind vs native bind
| Topic | _.bind | Function.prototype.bind |
|---|---|---|
this argument | Second parameter thisArg | First parameter |
| Partial arguments | Yes; supports placeholders | Yes; fixed order only |
Result length | Not adjusted (Lodash note) | Reduced toward arity |
For methods whose identity lives on an object key, see _.bindKey.
Pitfalls to avoid
Libraries reading fn.length
Lodash bind does not mimic native length trimming—validators that depend on arity may disagree.
Wrong sentinel
Use the same placeholder object your build expects (bind.placeholder or the shared Lodash _ export)—random objects will not merge arguments.
Lexical this
Arrow bodies ignore a bound thisArg; pass standard functions when binding matters.
❓ FAQ
Summary
- Purpose:
_.bind(func, thisArg, ...partials)fixesthisand optionally curries leading parameters with placeholder support. - Contrast: Native
bindcannot defer arbitrary slots;_.partialomits an explicitthisArgslot. - Next: Lodash _.bindKey(), _.before(), or official Lodash docs for _.bind.
Lodash’s bind builds on an internal createWrap pipeline with bind and partial bitmasks—placeholders are swapped at call time via replaceHolders, which is why skipping argument positions works the same across bind, partial, and friends.
6 people found this page helpful
