Syntax, Parameter and Return Value
Syntax:
_.functions(object)
Parameters:
object (Object): The object to inspect.
Return Value:
(Array) - Returns the array of function names.
Examples
Listing Function Properties of an Object:
JavaScript
var _ = require('lodash');
var myObject = {
a: 1,
b: 'string',
c: function() { console.log('c'); },
d: function() { console.log('d'); }
};
console.log(_.functions(myObject));
You can also try this code with Online Javascript Compiler
Run Code
Output:
['c', 'd']
Demonstrates retrieving the names of function properties in an object.
Dynamic Method Invocation:
JavaScript
var operations = {
add: function(x, y) { return x + y; },
subtract: function(x, y) { return x - y; }
};
_.functions(operations).forEach(function(methodName) {
console.log(methodName, operations[methodName](5, 3));
});
You can also try this code with Online Javascript Compiler
Run Code
Output:
'add' 8, 'subtract' 2
Shows how to dynamically invoke methods listed by _.functions().
Filtering Methods for Documentation:
JavaScript
var api = {
getVersion: function() {},
loadData: function() {},
_privateMethod: function() {}
};
var publicMethods = _.functions(api).filter(name => !name.startsWith('_'));
console.log(publicMethods); var api = {
getVersion: function() {},
loadData: function() {},
_privateMethod: function() {}
};
var publicMethods = _.functions(api).filter(name => !name.startsWith('_'));
console.log(publicMethods);
You can also try this code with Online Javascript Compiler
Run Code
Output:
['getVersion', 'loadData']
An example of filtering out private methods (conventionally prefixed with '_') for documentation purposes.
Integrating with Other Lodash Methods:
JavaScript
var complexObject = {
prop: 'value',
action: function() {},
anotherAction: function() {}
};
var methodNames = _.chain(complexObject)
.functions()
.sort()
.value();
console.log(methodNames);
You can also try this code with Online Javascript Compiler
Run Code
Output:
['action', 'anotherAction']
Demonstrates combining _.functions() with other Lodash methods for custom processing.
Frequently Asked Questions
How does _.functions() differ from Object.keys()?
Object.keys() returns all own enumerable property names of an object, regardless of whether they are functions or not, while _.functions() specifically returns names of function properties.
Does _.functions() include inherited methods?
No, _.functions() only includes the function properties that are own properties of the object. It does not include inherited methods.
Can _.functions() be used with arrays?
While it can technically be used with arrays, _.functions() is most useful with objects, as arrays are typically used for storing data rather than methods.
Conclusion
Lodash's _.functions() method is a valuable tool for identifying the callable methods of an object. It's particularly useful for introspection, dynamic method invocation, and API exploration, helping developers understand and utilize the capabilities of an object.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.