Lodash partial vs Native Function bind
Both Lodash’s _.partial and JavaScript's native
Function.prototype.bind are used for partial application,
allowing you to pre-fill a function's arguments. However, they differ
fundamentally in how they handle the execution context
(this), how they order and fill arguments, and their
flexibility with placeholders. While
Function.prototype.bind is primarily designed to lock a
function's this context while optionally pre-filling
arguments, _.partial is designed purely for argument
pre-filling without altering the execution context, while also
supporting argument placeholders.
1. Handling of the
this Context
The most critical distinction between the two methods is how they
manage this.
Function.prototype.bind: The first argument passed to.bind()explicitly sets and locks thethiscontext for the returned function. Once bound, the context cannot be overridden, even if invoked as a method of another object or with.call()/.apply()._.partial: It completely ignores thethisbinding step. The returned function preserves whatever context it is executed in at runtime. If the partially applied function is attached as a method to an object,thiswill point to that object as expected.
function greet(greeting, punctuation) {
return `${greeting}, ${this.name}${punctuation}`;
}
const user = { name: "Alice" };
// Native bind locks the context as its first argument
const boundGreet = greet.bind(user, "Hello");
console.log(boundGreet("!")); // "Hello, Alice!"
// Lodash partial does not bind 'this'
user.partialGreet = _.partial(greet, "Hello");
console.log(user.partialGreet("!")); // "Hello, Alice!"2. Support for Placeholders
Native Function.prototype.bind applies arguments
strictly from left to right. You cannot skip an argument to be supplied
later.
Lodash provides a placeholder feature (_ or
_.partial.placeholder), allowing you to pre-fill arguments
in any position, not just sequentially from the left:
function buildUrl(protocol, domain, path) {
return `${protocol}://${domain}/${path}`;
}
// Pre-fill the first and third arguments, leaving the middle for later
const getSecuredDoc = _.partial(buildUrl, "https", _, "index.html");
console.log(getSecuredDoc("example.com"));
// Output: "https://example.com/index.html"To achieve this with native JavaScript, you must wrap the call in a custom closure or arrow function.
3. Argument
Placement Flexibility (_.partialRight)
While bind strictly appends incoming arguments to the
end of the pre-filled arguments, Lodash also offers
_.partialRight. This companion function allows arguments to
be partially applied from the right side of the parameter list:
function divide(a, b) {
return a / b;
}
const half = _.partialRight(divide, 2);
console.log(half(10)); // 5 (10 / 2)Summary of Differences
- Context (
this):bindexplicitly sets and locksthis;_.partialleavesthisdynamic based on the call site. - First Parameter:
bindrequiresthisArgas its first parameter;_.partialtakes the target function as its first argument followed immediately by the partially applied values. - Placeholders:
_.partialsupports placeholder tokens to skip arguments;bindonly applies arguments sequentially from left to right.