Reverse of Lodash methodOf: Understanding _.method
In the Lodash JavaScript library, the function that provides the
reverse behavior of _.methodOf is _.method.
While _.methodOf creates a function that takes a property
path and invokes it on a pre-defined object, _.method does
the exact opposite by pre-defining the method path (and optional
arguments) and returning a function that accepts the target object. This
article breaks down the mechanics of both functions, highlights their
inversion of control, and demonstrates how to use _.method
effectively in functional JavaScript workflows.
Understanding
_.methodOf
To understand the reverse behavior, consider how
_.methodOf operates:
_.methodOf(object, [args])With _.methodOf, you supply the target
object first. Lodash returns a function that expects a
path (the method name or property path to invoke).
const user = {
getName: () => 'Alice',
getRole: () => 'Admin'
};
const invokeOnUser = _.methodOf(user);
invokeOnUser('getName'); // Returns 'Alice'
invokeOnUser('getRole'); // Returns 'Admin'This pattern is useful when you have a single object and want to dynamically select which method to run based on incoming identifiers.
The Reverse Behavior:
_.method
The inverse counterpart is _.method:
_.method(path, [args])With _.method, the parameter order and execution flow
are flipped. You specify the method path and any default
arguments upfront. Lodash returns a function that expects the target
object.
const getName = _.method('getName');
const userA = { getName: () => 'Alice' };
const userB = { getName: () => 'Bob' };
getName(userA); // Returns 'Alice'
getName(userB); // Returns 'Bob'Key Differences at a Glance
| Feature | _.methodOf(object, [args]) |
_.method(path, [args]) |
|---|---|---|
| First Argument | The target object | The method path (string or array) |
| Returned Function Accepts | The method path | The target object |
| Typical Use Case | Querying multiple methods on one object | Applying one method across multiple objects |
Practical Use Case for
_.method
Because _.method returns a function that accepts an
object, it is ideally suited for collection iteration, such as mapping
or filtering arrays of objects with matching interfaces:
const users = [
{ id: 1, getStatus: () => 'active' },
{ id: 2, getStatus: () => 'pending' },
{ id: 3, getStatus: () => 'suspended' }
];
// Using _.method directly in an array map
const statuses = users.map(_.method('getStatus'));
// Result: ['active', 'pending', 'suspended']You can also pass arguments to the invoked method:
const items = [
{ calculateTotal: (tax) => 100 + tax },
{ calculateTotal: (tax) => 200 + tax }
];
const calculateWithStandardTax = _.method('calculateTotal', 15);
items.map(calculateWithStandardTax);
// Result: [115, 215]Conclusion
If your application architecture requires binding an operation to a
method name and iterating over many instances, use
_.method. If you instead need to fix an instance and query
different methods on demand, _.methodOf is the appropriate
complementary utility.