How Lodash _.invoke Executes Functions at an Object Path
This article explains how the _.invoke method in the
Lodash JavaScript library navigates nested object structures, safely
resolves target functions at specified paths, and executes them with
provided arguments. You will learn the internal mechanics of path
resolution, context binding, and the safe-execution guarantees provided
by this utility function.
The Syntax and Purpose of
_.invoke
The _.invoke method allows developers to call a method
located deeply within an object without having to manually check for the
existence of each intermediate property.
Its signature is:
_.invoke(object, path, [args])object: The target object to query.path: The path of the method to invoke, specified as a string (e.g.,'a.b.c') or an array of keys (e.g.,['a', 'b', 'c']).[args]: Optional arguments passed directly into the invoked function.
The method returns the result of the invoked function, or
undefined if the path does not exist or the target property
is not a callable function.
Internal Execution Mechanics
Lodash processes _.invoke through a structured sequence
of internal steps:
1. Path Parsing and Traversal
Lodash converts the provided path into an array of
property keys using its internal path-to-key parser (the same mechanism
powering _.get). For example, the path
'users[0].getName' is normalized into
['users', '0', 'getName'].
Lodash iterates through the keys up to the second-to-last key to navigate down the object hierarchy and locate the parent object that holds the target method.
2. Context Determination
(this Binding)
In JavaScript, executing an object method often requires the correct
this reference. Lodash separates the path into:
- The parent object (the value resolved up to the penultimate key).
- The method key (the final element of the path array).
When the method is executed, Lodash ensures the parent object is
supplied as the this context, preserving the expected
behavior of object-oriented methods.
3. Safe Type Checking
Before invoking the target, Lodash checks if the resolved value at
the final key is actually a function. If any intermediate property in
the path is null or undefined, or if the final
resolved property is not a function, Lodash short-circuits the
operation. Instead of throwing a runtime TypeError (such as
is not a function), it safely returns
undefined.
4. Method Invocation with Arguments
If the target is a valid function, Lodash invokes it using
JavaScript's native .apply() or .call()
methods, forwarding the parent object as this and unpacking
any remaining args passed to _.invoke.
Practical Example
Consider the following nested object:
const company = {
departments: {
engineering: {
teamLead: "Alice",
announce(greeting, punctuation) {
return `${greeting}, I am ${this.teamLead}${punctuation}`;
}
}
}
};Using _.invoke to execute announce:
const result = _.invoke(
company,
'departments.engineering.announce',
'Hello',
'!'
);
console.log(result);
// Output: "Hello, I am Alice!"If you attempt to invoke a path that does not exist or is not a function:
const missing = _.invoke(company, 'departments.hr.announce', 'Hello');
console.log(missing);
// Output: undefined (no error thrown)By decoupling path navigation from invocation while handling edge
cases such as missing properties and incorrect this
contexts, _.invoke provides a resilient way to call dynamic
and deeply nested functions in JavaScript.