Syntax, Parameter and Return Value
Syntax:
_.bindKey(object, key, [partials])
Parameters:
-
object: The object to bind and assign the method to.
-
key: The key of the method in the object.
- [partials]: Arguments to be partially applied (fixed) in the new function.
Return Value:
(Function) - Returns the new bound function.
Examples
Binding a Method to Its Object:
JavaScript
var _ = require('lodash');
var user = {
name: 'John',
greet: function(greeting) {
console.log(greeting + ', ' + this.name);
}
};
var boundGreet = _.bindKey(user, 'greet', 'Hello');
boundGreet();

You can also try this code with Online Javascript Compiler
Run Code
Output:
'Hello, John'
Demonstrates binding a method to its owning object and partially applying an argument.
Using in Event Handlers:
JavaScript
var button = {
label: 'Submit',
onClick: function() { console.log(this.label + ' clicked'); }
};
var boundClickHandler = _.bindKey(button, 'onClick');
document.getElementById('submitButton').addEventListener('click', boundClickHandler);

You can also try this code with Online Javascript Compiler
Run Code
// When the button is clicked:
Outputs
'Submit clicked'
Shows using _.bindKey() for event handlers to maintain the correct context.
Asynchronous Operations:
JavaScript
var asyncOperation = {
result: 'Success',
complete: function(callback) {
// Simulate async operation
setTimeout(() => callback(this.result), 1000);
}
};
var boundComplete = _.bindKey(asyncOperation, 'complete', result => console.log(result));
boundComplete();

You can also try this code with Online Javascript Compiler
Run Code
Output after 1 second:
'Success'
An example of using _.bindKey() in an asynchronous operation to ensure the correct context.
Handling Dynamic Method Updates:
JavaScript
var person = {
name: 'Alice',
greet: function() { console.log('Hello, ' + this.name); }
};
var boundGreet = _.bindKey(person, 'greet');
// Updating the method later
person.greet = function() { console.log('Greetings, ' + this.name); };
boundGreet();

You can also try this code with Online Javascript Compiler
Run Code
Output:
'Greetings, Alice'
Demonstrates _.bindKey() handling updates to the method after binding.
Frequently Asked Questions
How is _.bindKey() different from _.bind()?
While _.bind() binds a function to a specific context, _.bindKey() binds a method of an object to the object itself, ensuring that the method retains access to the object's properties.
What happens if the object's method changes after using _.bindKey()?
If the method on the object changes after _.bindKey() is used, the bound function will invoke the updated method, maintaining the binding to the current method at that key.
Can _.bindKey() be used for non-method properties?
_.bindKey() is designed for methods (functions) of objects. Using it with non-method properties will not produce meaningful results.
Conclusion
Lodash's _.bindKey() method is a valuable tool for binding methods to their parent objects, ensuring consistent context (this value) in various scenarios, including event handling and asynchronous operations. It offers flexibility in managing method execution, especially in dynamic contexts.
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.