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
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));
You can also try this code with Online Javascript Compiler
Run Code
Output:
['childMethod', 'parentMethod']
Demonstrates retrieving both own and inherited function properties of an object.
Dynamic Method Invocation Including Inherited Methods:
JavaScript
var shape = {
draw: function() { console.log('Drawing'); }
};
Object.setPrototypeOf(shape, {
erase: function() { console.log('Erasing'); }
});
_.functionsIn(shape).forEach(function(methodName) {
shape[methodName]();
});
You can also try this code with Online Javascript Compiler
Run Code
Output:
'Drawing', 'Erasing'
Shows dynamically invoking all methods, including inherited ones.
Comprehensive API Exploration:
JavaScript
var api = {
getData: function() {},
_privateMethod: function() {}
};
Object.setPrototypeOf(api, {
refreshData: function() {}
});
var allMethods = _.functionsIn(api);
console.log(allMethods);
You can also try this code with Online Javascript Compiler
Run Code
Output:
['getData', '_privateMethod', 'refreshData']
An example of getting a complete list of methods for API exploration, including inherited ones.
Filtering Methods for Documentation:
JavaScript
var widget = {
show: function() {},
_internalUpdate: function() {}
};
Object.setPrototypeOf(widget, {
hide: function() {}
});
var publicMethods = _.functionsIn(widget).filter(name => !name.startsWith('_'));
console.log(publicMethods);
You can also try this code with Online Javascript Compiler
Run Code
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 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.