Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax:
3.2.
Parameters:
3.3.
Return Value: 
4.
Examples 
4.1.
Invoking Object Methods Dynamically:
4.2.
JavaScript
4.3.
Using with Arrays of Objects:
4.4.
JavaScript
4.5.
Accessor for Nested Object Methods:
4.6.
JavaScript
4.7.
Conditional Method Invocation:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
How does _.methodOf() differ from _.method()?
5.2.
Can _.methodOf() handle complex paths for method access?
5.3.
Is _.methodOf() suitable for all objects and methods?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.methodOf() Method

Author Rinki Deka
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In JavaScript, particularly in scenarios involving objects and their methods, there's often a need to dynamically call a method based on a given context. Lodash's _.methodOf() function addresses this by creating a function that calls a method of a provided object. 

Lodash _.methodOf() Method

This method is especially useful in cases where you need to invoke a method from an object based on certain conditions or inputs, and where the object acts as the context for the method.

Why This Function is Used

The _.methodOf() function is used to create a function that invokes the method at a given path of an object, with the object itself serving as the context. This is useful when the method to be invoked needs to be determined dynamically at runtime, and when the object context is central to the method's execution.

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

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

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

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

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 DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass