Syntax, Parameter and Return Value
Syntax:
_.method(path, [args])
Parameters:
-
path (Array|string): The path of the method to invoke.
- [args] (...*): The arguments to invoke the method with.
Return Value:
(Function) - Returns the new invoker function.
Examples
Dynamic Method Invocation:
JavaScript
var _ = require('lodash');
var object = {
data: [1, 2, 3],
getData: function(index) { return this.data[index]; }
};
var getDataMethod = _.method('getData', 1);
console.log(getDataMethod(object));
You can also try this code with Online Javascript Compiler
Run Code
Output:
2
Demonstrates creating a function to invoke a method of an object with a specific argument.
Using with Collections:
JavaScript
var users = [
{ 'user': 'barney', 'age': 36, 'greet': function() { return 'Hi ' + this.user; } },
{ 'user': 'fred', 'age': 40, 'greet': function() { return 'Hello ' + this.user; } }
];
var greetMethod = _.method('greet');
var greetings = _.map(users, greetMethod);
console.log(greetings);
You can also try this code with Online Javascript Compiler
Run Code
Output:
['Hi barney', 'Hello fred']
Shows creating a function to invoke a method on each object in a collection.
Accessing Nested Object Methods:
JavaScript
var store = {
checkout: {
processPayment: function(amount) { return 'Processed ' + amount; }
}
};
var processPayment = _.method(['checkout', 'processPayment'], 100);
console.log(processPayment(store));
You can also try this code with Online Javascript Compiler
Run Code
Output:
'Processed 100'
An example of using _.method() to access and invoke a method of a nested object.
Conditional Method Execution:
JavaScript
var rectangle = {
width: 10,
height: 20,
area: function() { return this.width * this.height; },
perimeter: function() { return 2 * (this.width + this.height); }
};
var getArea = _.method('area');
var getPerimeter = _.method('perimeter');
console.log(getArea(rectangle));
console.log(getPerimeter(rectangle));
You can also try this code with Online Javascript Compiler
Run Code
Output:
200
60
Demonstrates using _.method() for conditional execution of object methods based on runtime requirements.
Frequently Asked Questions
Can _.method() handle methods with multiple arguments?
Yes, _.method() can handle methods with multiple arguments by specifying them in the [args] parameter.
How does _.method() differ from directly invoking object methods?
_.method() provides a way to create a reusable function that can dynamically invoke object methods, which is particularly useful in higher-order functions or when the object or method is not known until runtime.
Is _.method() suitable for all objects and methods?
_.method() is suitable for most objects and methods, but it requires that the method path is correctly specified and the object structure adheres to the expected format.
Conclusion
Lodash's _.method() function is a handy tool for creating functions that invoke specified methods on objects, adding flexibility and dynamism to method invocation, especially useful in functional programming patterns and when dealing with dynamic or variable object structures.
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.