Lodash _.functions() method
What you’ll learn
- How
_.functions(object)returns the names of an object’s own enumerable function properties. - Why
_.functions(new MyClass())returns[]—and when to reach for_.functionsIninstead. - Practical patterns: discovering a plain-object API, validating module shape, and dispatching by name.
- Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Understand the difference between own properties and properties inherited via the prototype chain—this method’s entire personality is “own only.”
- Class methods live on the prototype: a value declared inside
classis not an own property of an instance. - Enumerable means visible to
Object.keys: non-enumerable methods (like inherited ones defined viaObject.definePropertywithoutenumerable: true) are skipped. - Returns an array of strings: not the function values themselves—just their names.
Overview
_.functions scans an object’s own enumerable string keys, keeps the ones whose value is a function, and returns those names in Lodash key iteration order. It’s a pure inspection helper—no mutation, no side effects, no recursion into the prototype chain.
Own keys only
Prototype methods are not included—the most common surprise on this method.
Key-order result
For plain objects, expect the same practical order you get from own-key iteration. Sort explicitly when needed.
Names, not values
Use the names to dispatch (obj[name](...)), document the API surface, or assert against an expected list.
Syntax
_.functions(object) - object: the object to inspect.
- Returns: an array of own enumerable function property names.
- Does not include: inherited properties, symbol keys, or non-enumerable properties.
List own function properties from a plain object
For a plain object literal, every method is an own property, so they all show up in own-key iteration order.
import functions from "lodash/functions";
const sampleObject = {
name: "John Doe",
age: 30,
sayHello() {
console.log("Hello!");
},
calculate(a, b) {
return a + b;
}
};
functions(sampleObject);
// -> ["sayHello", "calculate"]
// ^^^^^^^^^^ ^^^^^^^^^
// own function names only Prototype methods: own vs inherited
Methods placed on a constructor’s prototype are inherited, not own, so _.functions skips them. _.functionsIn walks the prototype chain and reports those enumerable inherited methods. (ES class methods are non-enumerable, so they hide from both helpers—see the pitfalls section below.)
import functions from "lodash/functions";
import functionsIn from "lodash/functionsIn";
function MathOperations() {}
MathOperations.prototype.add = function (a, b) { return a + b; };
MathOperations.prototype.subtract = function (a, b) { return a - b; };
const math = new MathOperations();
functions(math);
// -> []
// add / subtract live on MathOperations.prototype, not on `math`.
functionsIn(math);
// -> ["add", "subtract"]
// *In variants walk the prototype chain (enumerable keys only). Validate a module’s required surface
A perfect use case: assert that an object exposes a known set of method names before plugging it into a pipeline.
import functions from "lodash/functions";
const authModule = {
login: async () => { /* ... */ },
logout: async () => { /* ... */ },
fetchData: async () => { /* ... */ },
baseUrl: "https://api.example.com" // not a function
};
const required = ["login", "logout", "fetchData"];
const exposed = functions(authModule);
// exposed -> ["login", "logout", "fetchData"]
const missing = required.filter((name) => !exposed.includes(name));
// missing -> [] () 📋 _.functions vs _.functionsIn vs Object.keys + filter
| Topic | _.functions | _.functionsIn | Object.keys(...).filter(...) |
|---|---|---|---|
| Own properties | Yes | Yes | Yes |
| Inherited enumerable properties | No | Yes | No |
| Filters non-functions | Built-in | Built-in | Manual with typeof === "function" |
| Result order | Sorted | Sorted | Insertion order |
| Returns | Array of names | Array of names | Array of names |
Reach for _.functions on plain data objects. Switch to _.functionsIn the moment class instances or mixin-based composition is involved.
Pitfalls to avoid
Class instances look empty
_.functions(new MyClass()) returns [] because methods live on the prototype. ES class methods are also non-enumerable, so _.functionsIn may still return [].
Order is not an API contract for workflows
If method order matters, sort the returned names explicitly or maintain an ordered allow-list.
Returns names, not the functions themselves
If you need callable references, map back: functions(obj).map((name) => obj[name]).
Symbol-keyed methods are skipped
If your API uses well-known symbols (like Symbol.iterator), pair this with Object.getOwnPropertySymbols.
❓ FAQ
Summary
- Purpose: return the names of an object’s own enumerable function properties.
- Remember: own enumerable keys only; ES class methods are non-enumerable; sort the result yourself if order matters.
- Next: Lodash _.functionsIn(), or the official Lodash docs for _.functions.
_.functions only looks at own enumerable properties. ES class methods live on the prototype and are non-enumerable, so _.functions(new MyClass()) returns []; _.functionsIn can include inherited methods only when they are enumerable.
6 people found this page helpful
