Lodash _.methodOf() Method

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 6 Examples + 3 Try It
Util utilities

What You’ll Learn

By the end of this tutorial, you’ll use Lodash’s _.methodOf() to fix an object and invoke different method paths on it at runtime.

01

Core Syntax

_.methodOf(object, ...args)

02

Path Later

Returned fn takes the method path.

03

Partial Args

Bind method args at creation.

04

Dispatch Tables

Pick operation name at runtime.

05

vs method

Object first vs path first.

06

this Binding

Context stays on the object.

What Is _.methodOf()?

_.methodOf() is the mirror of _.method(). You fix the object upfront; the returned function accepts a method path and calls that method on the object—with correct this binding.

💡
Beginner tip — path goes on the invoker call

_.methodOf(user)('greet') calls user.greet(). Do not write _.methodOf(user, 'greet')—the second argument to methodOf is a method argument, not the path name.

Use this when one instance exposes many operations and you choose the method name dynamically—calculator ops, plugin APIs, or command routers.

📝 Syntax

Fix the object (and optional method args); pass the path when you call the invoker:

javascript
_.methodOf(object, [args])

Syntax Rules

  • object — the target whose methods will be invoked.
  • args — optional arguments forwarded to the method (bound at creation).
  • Return value — function (path) => result.
  • path — method name or dot-path passed to the returned function.
  • Mirror_.method(path, ...args)(object)_.methodOf(object, ...args)(path).
javascript
import methodOf from "lodash/methodOf";



const user = {

  name: "John",

  greet() {

    return "Hello, " + this.name + "!";

  },

};



const invokeOnUser = methodOf(user);



invokeOnUser("greet");

// "Hello, John!"

⚡ Quick Reference

TaskCode patternNotes
Build invoker_.methodOf(obj)Object fixed
Call methodinvoker('methodName')Path argument
With method args_.methodOf(calc, 10, 5)('add')Args bound early
Nested path_.methodOf(root)('api.fetch')Dot path
Path-first twin_.method('add', 10, 5)(calc)Same invoke
One-off_.invoke(obj, 'greet')No factory
Returns
Function

Path invoker

Fixes
Object

Target instance

Opposite
method

Path first

Category
Util

Invocation

🧰 Parameters

Arguments to _.methodOf() and the invoker it returns:

object Required

The instance whose methods will be called. Closed over by the returned invoker.

_.methodOf(calculator)
args Optional

Values passed to the method when the invoker runs. Set on _.methodOf, not on invoker(path).

_.methodOf(calc, 10, 5)
path Invoker arg

Method path supplied when you call the returned function—e.g. 'greet' or 'api.fetch'.

invokeOnUser("greet")
missing path undefined

If the path is missing or not callable, invoke returns undefined. Guard with _.has or a typeof check.

invokeOnUser("missing")

When the path is fixed and objects vary, use _.method() instead.

Examples Gallery

Practical _.methodOf() patterns with copy-ready code, sample output, and interactive Try It Yourself labs.

📚 Getting Started

Basic greet and dynamic operation dispatch.

Example 1 — Greet on a fixed user

Fix the object, pass the method path to the invoker.

javascript
const user = {

  name: "John",

  greet() {

    return "Hello, " + this.name + "!";

  },

};



const invokeOnUser = _.methodOf(user);



console.log(invokeOnUser("greet"));

// "Hello, John!"
Try It Yourself

How It Works

The old tutorial used _.methodOf(user, 'greet') then greetingFunction()—that misplaces the path. The path belongs on the invoker call: invokeOnUser('greet').

Example 2 — Dynamic operation dispatch

One math utils object; pick square or cube at runtime.

javascript
const mathUtils = {

  square(n) { return n * n; },

  cube(n) { return n * n * n; },

};



const calculate = (operation, num) =>

  _.methodOf(mathUtils, num)(operation);



console.log(calculate("square", 3)); // 9

console.log(calculate("cube", 3));   // 27
Try It Yourself

How It Works

Bind the numeric operand with _.methodOf(mathUtils, num), then pass the operation name as the path—clean dispatch without a large switch statement.

📈 Practical Patterns

Partial args, nested paths, and comparison with method.

Example 3 — Calculator with bound operands

Bind method arguments when creating the invoker; path selects add vs subtract.

javascript
const calculator = {

  value: 0,

  add(n) {

    this.value += n;

    return this.value;

  },

};



const addFive = _.methodOf(calculator, 5);



console.log(addFive("add"));

// 5 — this.value updated on calculator

How It Works

this stays on calculator automatically—no manual .call(calculator, …) needed. The old reference’s addFive.call(calculator, 5) pattern was redundant and misused the API.

Example 4 — Nested method path

Invoke a method on a nested object via dot path.

javascript
const service = {

  api: {

    ping() { return "pong"; },

  },

};



const invokeOnService = _.methodOf(service);



console.log(invokeOnService("api.ping"));

// "pong"
Try It Yourself

How It Works

Path resolution matches _.invoke—traverse nested properties before calling the method.

🚀 Beyond the Basics

Mirror of method and safe invocation.

Example 5 — methodOf vs method

Two ways to invoke the same method with the same partial args.

javascript
const calc = {

  add(a, b) { return a + b; },

};



console.log(_.methodOf(calc, 10, 5)("add"));

console.log(_.method("add", 10, 5)(calc));

// both: 15

When to use which

Use methodOf when the object is stable and paths vary. Use method when the path is stable and you map over many objects.

Example 6 — Guard before invoking

Validate that the path exists on the object.

javascript
const car = {

  brand: "Toyota",

  startEngine() {

    return "Engine started.";

  },

};



const invokeOnCar = _.methodOf(car);

const path = "startEngine";



if (typeof _.get(car, path) === "function") {

  console.log(invokeOnCar(path));

} else {

  console.log("Method not found.");

}

How It Works

Using _.has(car, 'startEngine') before building the invoker is fine too. Avoid the old anti-pattern of composing with window.alert via methodOf—it couples code to globals unnecessarily.

🧠 How _.methodOf() Works

1

Capture object + args

Lodash closes over the target object and any method arguments.

Setup
2

Return path invoker

The new function waits for a method path string or key array.

Factory
3

Resolve & invoke

Find the method at path on the fixed object, call with bound args and correct this.

Execute
=

Method return value

Whatever the invoked method returns—or undefined if the path is missing.

📝 Notes

  • The method path is not the second argument to _.methodOf—it is the argument to the returned function.
  • Arguments after object in _.methodOf are forwarded to the method, like _.method.
  • this is set to the fixed object automatically—no extra .call needed.
  • Prefer _.method() for _.map(collections, _.method('fn')) patterns.
  • Missing paths return undefined from invoke—validate dynamic path strings in user-facing dispatchers.
  • Next in the series: _.mixin() for extending Lodash itself.

Conclusion

_.methodOf() fixes an object and lets you invoke different method paths on it— the object-first mirror of _.method().

Use it for dispatch tables and plugin APIs; bind operands with partial args and pass the operation name when you call the invoker.

💡 Best Practices

✅ Do

  • Pass paths to the invoker: invokeOn(obj)('methodName')
  • Bind method args in _.methodOf(object, ...args)
  • Validate dynamic paths before dispatch in user-facing APIs
  • Use for operation routers on a single service instance
  • Pair with _.invoke for one-off calls

❌ Don’t

  • Pass the method name as the second arg to _.methodOf
  • Manually .call the object unless you know why
  • Use methodOf for map-over-objects—use method instead
  • Compose with global window methods in tutorials or app code
  • Assume missing methods throw—they often return undefined

Key Takeaways

Knowledge Unlocked

Five things to remember about _.methodOf()

Use these points when building object-first invokers.

5
Core concepts
📂 02

Path later

On invoker call.

Mechanics
🔗 03

Bind args

At creation.

Partial
🛠 04

Dispatch

Runtime paths.

Practical
05

method

Mirror helper.

Compare

❓ Frequently Asked Questions

_.methodOf(object, ...args) returns a function that takes a method path and invokes that method on the fixed object, forwarding any args you bound at creation. It is the object-first counterpart to _.method(path, ...args).
The path (method name or dot-path string) as its first argument—not the object. Example: _.methodOf(user)('greet') calls user.greet() with user as this.
Arguments after object in _.methodOf are bound when you create the invoker. Example: _.methodOf(calc, 10, 5)('add') calls calc.add(10, 5).
_.method(path, ...args) fixes the path—the object comes later. _.methodOf(object, ...args) fixes the object—the path comes later. They are mirror-image invoker factories.
No—that treats 'greet' as a method argument, not a path. Use _.methodOf(object)('greet') or _.method('greet')(object). The path is passed to the returned function, not to _.methodOf itself.
Use it when one object exposes several methods and you pick the path at runtime—calculator dispatch tables, plugin registries, or APIs where the target instance is fixed but the operation name varies.
Did you know?

_.methodOf(object, ...args)(path) and _.method(path, ...args)(object) are two views of the same invoke operation—pick whichever side of the call varies in your code.

Practice _.methodOf() in the Live Editor

Open the Try It editor, run the examples, and build object-first dispatch helpers.

Open Try It editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful