- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get all methods of any object JavaScript
We are required to write a program (function) that takes in an object reference and returns an array of all the methods (member functions) that lives on that object.We are only required to return the methods in the array and not any other property that might have value of type other than a function.
We will use the Object.getOwnPropertyNames function
The Object.getOwnPropertyNames() method returns an array of all properties (enumerable or not) found directly upon a given object. And then we will filter the array to contain property of data type 'function' only.
Example
const returnMethods = (obj = {}) => { const members = Object.getOwnPropertyNames(obj); const methods = members.filter(el => { return typeof obj[el] === 'function'; }) return methods; }; console.log(returnMethods(Array.prototype));
Output
And the output in the console will be −
[ 'constructor', 'concat', 'copyWithin', 'fill', 'find', 'findIndex', 'lastIndexOf', 'pop', 'push', 'reverse', 'shift', 'unshift', 'slice', 'sort', 'splice', 'includes', 'indexOf', 'join', 'keys', 'entries', 'values', 'forEach', 'filter', 'flat', 'flatMap', 'map', 'every', 'some', 'reduce', 'reduceRight', 'toLocaleString', 'toString' ]
- Related Articles
- JavaScript Object Methods
- Is there a way to print all methods of an object in JavaScript?
- JavaScript - Get Date Methods
- Get the list of all the declared methods in Java
- Get the list of all the public methods in Java
- How to add, access JavaScript object methods?
- What are the methods of a boolean object in JavaScript?
- What are the methods of an array object in JavaScript?
- Do you think JavaScript Functions are Object Methods?
- How to define methods for an Object in JavaScript?
- Get value of any attribute from XML data in JavaScript?
- How to get a list of all the test methods in a TestNG class?
- Get all substrings of a string in JavaScript recursively
- How to add properties and methods to an object in JavaScript?
- How to get the length of an object in JavaScript?

Advertisements