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 All Function Properties Including Inherited:
4.2.
JavaScript
4.3.
Dynamic Method Invocation Including Inherited Methods:
4.4.
JavaScript
4.5.
Comprehensive API Exploration:
4.6.
JavaScript
4.7.
Filtering Methods for Documentation:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
How does _.functionsIn() differ from _.functions()?
5.2.
Can _.functionsIn() be used with arrays?
5.3.
Is it recommended to use all methods returned by _.functionsIn()?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.functionsIn() Method

Author Riya Singh
0 upvote

Introduction

In JavaScript programming, especially when working with complex objects, it's often necessary to identify not only the object's own methods but also those inherited from its prototype chain. Lodash's _.functionsIn() method serves this purpose by providing a way to retrieve the names of all enumerable function properties of an object, including those inherited. 

Lodash _.functionsIn() Method

This method is particularly useful in object-oriented programming, debugging, or when you need a comprehensive view of the callable methods available on an object.

Why This Function is Used

The _.functionsIn() function is used to create an array of the names of all enumerable function properties, including those inherited, of an object. This is essential in situations where understanding the full range of an object's capabilities, including those derived from its prototype, is important. It helps in scenarios where you need to know all the actions (methods) that an object, along with its prototype chain, can perform.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Syntax, Parameter and Return Value

Syntax: 

_.functionsIn(object)

Parameters:

  • object (Object): The object to inspect.

Return Value: 

(Array) - Returns the array of function names.

Examples 

Listing All Function Properties Including Inherited:

  • JavaScript

JavaScript

var _ = require('lodash');

function Parent() {}

Parent.prototype.parentMethod = function() {};

function Child() {

 this.childMethod = function() {};

}

Child.prototype = Object.create(Parent.prototype);

var childObject = new Child();

console.log(_.functionsIn(childObject));

Output:

['childMethod', 'parentMethod']


Demonstrates retrieving both own and inherited function properties of an object.

Dynamic Method Invocation Including Inherited Methods:

  • JavaScript

JavaScript

var shape = {

 draw: function() { console.log('Drawing'); }

};

Object.setPrototypeOf(shape, {

 erase: function() { console.log('Erasing'); }

});

_.functionsIn(shape).forEach(function(methodName) {

 shape[methodName]();

});

Output: 

'Drawing', 'Erasing'


Shows dynamically invoking all methods, including inherited ones.

Comprehensive API Exploration:

  • JavaScript

JavaScript

var api = {

 getData: function() {},

 _privateMethod: function() {}

};

Object.setPrototypeOf(api, {

 refreshData: function() {}

});

var allMethods = _.functionsIn(api);

console.log(allMethods);

Output: 

['getData', '_privateMethod', 'refreshData']


An example of getting a complete list of methods for API exploration, including inherited ones.

Filtering Methods for Documentation:

  • JavaScript

JavaScript

var widget = {

 show: function() {},

 _internalUpdate: function() {}

};

Object.setPrototypeOf(widget, {

 hide: function() {}

});

var publicMethods = _.functionsIn(widget).filter(name => !name.startsWith('_'));

console.log(publicMethods);

 

Output: 

['show', 'hide']


Demonstrates filtering out private methods (conventionally prefixed with '_') for documentation purposes.

Frequently Asked Questions

How does _.functionsIn() differ from _.functions()?

_.functionsIn() retrieves both own and inherited function properties of an object, while _.functions() only considers the object's own function properties.

Can _.functionsIn() be used with arrays?

While it can technically be used with arrays, _.functionsIn() is more suitable for objects, as arrays are typically used for storing data rather than methods.

Is it recommended to use all methods returned by _.functionsIn()?

Care should be taken when invoking methods obtained from _.functionsIn(), especially inherited ones, as they might not always be intended for external use (e.g., private or internal methods).

Conclusion

Lodash's _.functionsIn() method is a valuable tool for comprehensively identifying the callable methods of an object, including those inherited through the prototype chain. It's particularly useful for understanding the full range of capabilities of an object in object-oriented programming and API exploration.

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.

Previous article
Lodash _.defaults() Method
Next article
Lodash _.at() Method
Live masterclass