Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax: 
3.2.
Parameters:
3.3.
Return Value:
4.
Examples 
4.1.
Listing Function Properties of an Object:
4.2.
JavaScript
4.3.
Dynamic Method Invocation:
4.4.
JavaScript
4.5.
Filtering Methods for Documentation:
4.6.
JavaScript
4.7.
Integrating with Other Lodash Methods:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
How does _.functions() differ from Object.keys()?
5.2.
Does _.functions() include inherited methods?
5.3.
Can _.functions() be used with arrays?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.functions() Method

Author Riya Singh
0 upvote

Introduction

In JavaScript, especially when working with objects that have a mix of properties and methods, it's often necessary to distinguish between the two. Lodash's _.functions() method addresses this need by providing an efficient way to retrieve the names of all the function properties of an object. 

Lodash _.functions() Method

This method is particularly useful for identifying the callable methods of an object, which can be crucial in various programming scenarios, such as dynamic method invocation or API exploration.

Why This Function is Used

The _.functions() function is used to create an array of function names present in an object. This is essential when you need to enumerate the methods of an object, whether for introspection, documentation, or runtime invocation purposes. It helps in understanding the capabilities of an object by listing all the actions (methods) that the object can perform.

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

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

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

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

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 DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass