Lodash _.partial() method
What you’ll learn
- How
_.partialfreezes leading arguments ahead of time. - How
partial.placeholderreserves gaps for later callers. - How partial differs from
bind,curry, and manual wrappers. - Pitfalls mixing modular placeholders with the monolithic
_export.
Prerequisites
Helpful reads: _.bind(), _.overArgs(), and the Function hub.
- Functions as values: returning callables from helpers.
- Arity: knowing which parameter each positional partial fills.
Overview
_.partial(func, ...partials) creates a function that calls func with your preset arguments merged ahead of runtime inputs—without forcing a this binding like bind.
Syntax
_.partial(func, ...partials)- func: original function to invoke.
- partials: values (or placeholders) applied before caller-supplied arguments.
- returns: wrapped function with merged arguments.
Freeze a leading operand
Build a unary helper from a binary without rewriting the core logic.
import partial from "lodash/partial";
function add(a, b) {
return a + b;
}
const addTen = partial(add, 10);
addTen(5); // 15Reserve a slot with partial.placeholder
Fix the right operand while letting the left arrive later.
import partial from "lodash/partial";
function multiply(a, b) {
return a * b;
}
const timesFive = partial(multiply, partial.placeholder, 5);
timesFive(8); // 40Prefix several literals
Great for URL builders or template segments shared across routes.
import partial from "lodash/partial";
function joinSegments(a, b, c) {
return [a, b, c].join("/");
}
const apiPath = partial(joinSegments, "api", "v2");
apiPath("users"); // "api/v2/users"📋 _.partial vs _.bind vs _.curry
| Helper | Primary focus | Pick when |
|---|---|---|
_.partial | Leading argument application | You only need frozen parameters, not this |
_.bind | this plus partial arguments | Methods must keep owner context |
_.curry | Progressive arity filling | You want multiple staged calls until arity completes |
Pitfalls to avoid
Wrong sentinel reference
Modular partial.placeholder differs from the monolithic _ export—never mix placeholders from different entry points.
thisMethods needing context
Partial does not bind this; wrap with bind or arrow-bound methods when object state matters.
Surprising argument order
Sketch parameter indices before stacking partials so placeholders line up with how callers will invoke the wrapper.
❓ FAQ
Summary
- Purpose:
_.partialapplies preset arguments before runtime inputs reachfunc. - Flex: placeholders defer specific slots until invocation.
- Next: Lodash _.partialRight(), revisit Lodash _.overArgs(), or read official _.partial docs.
Lodash implements _.partial through the same createWrap pathway as bind, so placeholder substitution (replaceHolders) behaves consistently—you can mix fixed values and deferred slots before later arguments append.
6 people found this page helpful
