Lodash _.wrap() method
What you’ll learn
- How the wrapper’s first argument is the wrapped function reference.
- Decorating behavior—sanitize HTML then wrap it in markup.
- Forwarding spread arguments through transparent decorators.
- When
partial/bindfits better thanwrap.
Prerequisites
Comfortable with _.partial(), _.bind(), and the Function hub.
- Higher-order functions: functions that accept or return other functions.
- Rest parameters:
(fn, ...args) => fn(...args)forwarding patterns.
Overview
_.wrap(func, wrapper) builds a callable that forwards outer arguments into wrapper, inserting func as the first parameter—classic decorator ergonomics without rewriting func itself.
Syntax
_.wrap(func, wrapper)- func: callable Lodash wraps and exposes as the wrapper’s first argument.
- wrapper: invoked as
wrapper(func, ...args)with samethisas the outer invocation unless otherwise fixed. - returns: new function applying that delegation rule.
Escape text, then add markup
Lodash’s cookbook pattern—delegate sanitizing to escape, then wrap the safe string.
import wrap from "lodash/wrap";
import escape from "lodash/escape";
const paragraph = wrap(escape, (fn, text) => `${fn(text)}
`);
paragraph("Tom & Jerry");
// "Tom & Jerry
"Transform the inner result
Call through then reshape output—independent of how many arguments the wrapped API expects.
import wrap from "lodash/wrap";
function greet(name) {
return `Hello, ${name}!`;
}
const shout = wrap(greet, (fn, name) => fn(name).toUpperCase());
shout("Ada");
// "HELLO, ADA!"Transparent proxies
Intercept arbitrary arity—summarize inputs without rewriting every caller.
import wrap from "lodash/wrap";
function sum(a, b) {
return a + b;
}
const audited = wrap(sum, (fn, ...args) => {
const logged = args.reduce((acc, n) => acc + n, 0);
const inner = fn(...args);
return { loggedSumHint: logged, result: inner };
});
audited(2, 3);
// { loggedSumHint: 5, result: 5 }📋 _.wrap vs _.partial vs _.bind
| Tool | Use when | Signature intuition |
|---|---|---|
_.wrap | Add behavior around each invocation | (fn, ...outer) → wrapper controls calling fn |
_.partial | Freeze leading arguments ahead of time | Partially applied curried slots |
_.bind | Pin this or mix placeholders | Bound receiver + partial args |
Pitfalls to avoid
Calling the wrong fn reference
Pass along Lodash’s injected callable—the closure capturing stale refs risks recursion surprises.
thisMethods on prototypes
Extract methods safely (bind, bound arrows) before wrapping when receivers carry hidden state.
Inference friction
Type declarations seldom encode Lodash’s injected-first-arg convention—document wrapper contracts by hand.
❓ FAQ
Summary
- Purpose:
_.wrapdecorates calls while keepingfuncreusable inside the wrapper. - Contract: wrapper receives
funcfirst, caller args second. - Next: Lodash _.now(), revisit Lodash _.unary(), or read official _.wrap docs.
Lodash passes the wrapped function reference as the first argument to your wrapper, followed by whatever arguments the outer caller supplied—so the wrapper controls whether and how func runs.
6 people found this page helpful
