Syntax, Parameter and Return Value
Syntax:
_.bind(func, thisArg, [partials])
Parameters:
-
func (Function): The function to bind.
-
thisArg: The value to bind as this.
- [partials]: Arguments to be partially applied (fixed) in the new function.
Return Value:
(Function) - Returns the new bound function.
4 Examples
Binding a Method to an Object:
JavaScript
var _ = require('lodash');
var user = {
name: 'John',
greet: function() {
console.log('Hello, ' + this.name);
}
};
setTimeout(_.bind(user.greet, user), 1000);

You can also try this code with Online Javascript Compiler
Run Code
Output after 1 second:
'Hello, John'
Demonstrates binding a method to its owning object for use with setTimeout.
Partial Application with Bind:
JavaScript
function greet(greeting, punctuation) {
console.log(greeting + ', ' + this.name + punctuation);
}
var greetJohn = _.bind(greet, { name: 'John' }, 'Hello');
greetJohn('!');

You can also try this code with Online Javascript Compiler
Run Code
Output:
'Hello, John!'
Shows binding a function with a pre-filled argument.
Event Handlers with Bind:
JavaScript
function handleClick(event) {
console.log(this.label + ' clicked');
}
var button = {
label: 'Submit',
onClick: _.bind(handleClick, { label: 'Submit' })
};
document.getElementById('submitButton').addEventListener('click', button.onClick);

You can also try this code with Online Javascript Compiler
Run Code
// When button is clicked:
Outputs:
'Submit clicked'
An example of using _.bind() for event handlers to maintain the correct context.
Binding Functions in Asynchronous Operations:
JavaScript
var dbQuery = function(callback) {
// Simulating a database query
setTimeout(callback, 2000);
};
var logResult = _.bind(function(result) {
console.log('Query result:', result);
}, null, 'Success');
dbQuery(logResult);

You can also try this code with Online Javascript Compiler
Run Code
Output after 2 seconds:
'Query result: Success'
Demonstrates using _.bind() for handling asynchronous callbacks with pre-filled data.
Frequently Asked Questions
How does _.bind() differ from native JavaScript Function.prototype.bind()?
Lodash's _.bind() is similar to the native Function.prototype.bind() but has additional capabilities, such as partial application of arguments and better handling of certain edge cases.
Can the thisArg in _.bind() be changed later?
Once a function is bound using _.bind(), the thisArg is permanently fixed and cannot be changed for that bound function.
Is _.bind() commonly used in modern JavaScript?
While _.bind() is still useful, especially for partial application, modern JavaScript arrow functions and their lexical this have reduced the need for explicit binding in many cases.
Conclusion
Lodash's _.bind() method offers a robust way to set the context of a function (this value) and pre-fill arguments, ensuring that the function behaves consistently in various scenarios. It is particularly useful in object-oriented programming and in cases where function context needs to be explicitly controlled.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.