Syntax, Parameter and Return Value
Syntax:
_.methodOf(object, [args])
Parameters:
-
object: The object to query for the method.
- [args] (...*): The arguments to invoke the method with.
Return Value:
(Function) - Returns the new invoker function.
Examples
Invoking Object Methods Dynamically:
JavaScript
var _ = require('lodash');
var obj = {
add: function(x, y) { return x + y; },
multiply: function(x, y) { return x * y; }
};
var callAdd = _.methodOf(obj, [1, 2]);
var callMultiply = _.methodOf(obj, [3, 4]);
console.log(callAdd('add'));
console.log(callMultiply('multiply'));

You can also try this code with Online Javascript Compiler
Run Code
Output:
3
12
Demonstrates creating functions to dynamically invoke different methods of an object.
Using with Arrays of Objects:
JavaScript
var operations = [
{ action: 'add', method: function(x, y) { return x + y; } },
{ action: 'subtract', method: function(x, y) { return x - y; } }
];
var callOperation = _.methodOf(operations, [5, 3]);
var results = operations.map(op => callOperation([op.action, 'method']));
console.log(results);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[8, 2]
Shows how to use _.methodOf() to invoke methods from an array of objects.
Accessor for Nested Object Methods:
JavaScript
var dataProcessor = {
process: {
start: function(data) { return 'Processing ' + data; },
end: function(result) { return 'Finished with ' + result; }
}
};
var startProcess = _.methodOf(dataProcessor, ['data']);
console.log(startProcess(['process', 'start']));

You can also try this code with Online Javascript Compiler
Run Code
Output:
'Processing data'
An example of using _.methodOf() to access and invoke methods of a nested object.
Conditional Method Invocation:
JavaScript
var shapes = {
circle: { area: function(r) { return Math.PI * r * r; } },
square: { area: function(s) { return s * s; } }
};
var getArea = _.methodOf(shapes);
console.log(getArea(['circle', 'area'], 5));
console.log(getArea(['square', 'area'], 5));

You can also try this code with Online Javascript Compiler
Run Code
Output:
78.53981633974483
25
Demonstrates conditional invocation of methods based on the shape type in a shapes object.
Frequently Asked Questions
How does _.methodOf() differ from _.method()?
_.methodOf() creates a function that invokes a method from a given object (the object is the context), whereas _.method() creates a function that expects an object to invoke a method on (the object is an argument).
Can _.methodOf() handle complex paths for method access?
Yes, _.methodOf() can handle complex paths to access nested methods in an object.
Is _.methodOf() suitable for all objects and methods?
_.methodOf() is versatile but requires the object's structure to be consistent with the path provided. If the object structure is unknown or variable, additional checks might be necessary.
Conclusion
Lodash's _.methodOf() function is a valuable tool for dynamically invoking methods of a provided object. It's particularly beneficial for scenarios requiring flexible and context-dependent method invocation, adding versatility to object method handling in functional programming patterns.
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.