How to list all methods of an object in JavaScript | Javascript get methods of Object

Objects in JavaScript are combinations of key/value pairs. The values can be made up of properties and methods and may include all other JavaScript data types, like strings, numbers, and Booleans. All javascript objects descend from the parent Object constructor.

An Object has many helpful built-in methods we can utilize and access to start working with individual objects straightforward. In this tutorial, you will learn How to list all methods of an object in JavaScript with examples from the below direct links.

JavaScript Object Methods – Example

var person = {
  firstName: "John",
  lastName : "Doe",
  id       : 5566,
  fullName : function() {
    return this.firstName + " " + this.lastName;
  }
};

JavaScript Methods

JavaScript Methods are operations that can be implemented on objects. The definition of a JavaScript method is a property containing a function.

Property Value
firstName John
lastName Doe
age 50
eyeColor blue
fullName function() {return this.firstName + ” ” + this.lastName;}

Do Check:

Accessing Object Methods of JavaScript

Here is the syntax to access the object method:

objectName.methodName()

List all Methods of an Object in JavaScript

We can use theObject.getOwnPropertyNames()function to get all the property names linked to an object.

Then we can filter the resulting array, to only include that property name if it’s a function.

We determine if it’s a function by usingtypeofon it.

For example here is how we might create a utility function to do what we need:

getMethods = (obj) => Object.getOwnPropertyNames(obj).filter(item => typeof obj[item] === 'function')

This lists only the methods defined on that specific object, not any method defined in its prototype chain.

To do that we must take a slightly different route. We must first iterate the prototype chain and list all the properties in an array. Then we check if each single property is a function.

An easy way to make sure we don’t duplicate methods as we navigate the prototype chain (like constructor which is always present), we use a Set data structure that makes sure values are unique:

const getMethods = (obj) => {
  let properties = new Set()
  let currentObj = obj
  do {
    Object.getOwnPropertyNames(currentObj).map(item => properties.add(item))
  } while ((currentObj = Object.getPrototypeOf(currentObj)))
  return [...properties.keys()].filter(item => typeof obj[item] === 'function')
}

Example usage:

getMethods("")
getMethods(new String('test'))
getMethods({})
getMethods(Date.prototype)

Example on Get all methods of any object JavaScript

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:

Here is the output in the console:

[
'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'
]