Get all methods of any object JavaScript

In JavaScript, you can extract all methods (functions) from any object using Object.getOwnPropertyNames() combined with type filtering. This is useful for debugging, reflection, or dynamically working with object methods.

Understanding the Problem

Objects contain both properties (data) and methods (functions). To get only the methods, we need to:

  • Get all property names from the object
  • Filter properties where the value type is "function"
  • Return an array of method names

Using Object.getOwnPropertyNames()

The Object.getOwnPropertyNames() method returns an array of all properties (enumerable and non-enumerable) found directly on the given object. We then filter this array to include only properties of type "function".

Example: Get Array Methods

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

Example: Get Methods from Custom Object

const myObject = {
    name: "Test Object",
    value: 42,
    getName: function() { return this.name; },
    getValue: function() { return this.value; },
    calculate: function(x, y) { return x + y; }
};

console.log("All properties:", Object.getOwnPropertyNames(myObject));
console.log("Only methods:", returnMethods(myObject));
All properties: [ 'name', 'value', 'getName', 'getValue', 'calculate' ]
Only methods: [ 'getName', 'getValue', 'calculate' ]

Alternative: Include Inherited Methods

To get methods from the entire prototype chain, use for...in loop:

const getAllMethods = (obj) => {
    const methods = [];
    for (let prop in obj) {
        if (typeof obj[prop] === 'function') {
            methods.push(prop);
        }
    }
    return methods;
};

const arr = [1, 2, 3];
console.log("Own + inherited methods:", getAllMethods(arr).slice(0, 10));
Own + inherited methods: [
  'constructor', 'concat', 'copyWithin', 'fill',
  'find', 'findIndex', 'lastIndexOf', 'pop',
  'push', 'reverse'
]

Comparison of Approaches

Method Own Properties Inherited Properties Non-enumerable
Object.getOwnPropertyNames() Yes No Yes
for...in loop Yes Yes No

Conclusion

Use Object.getOwnPropertyNames() with type filtering to extract methods from objects. This technique is valuable for object inspection, debugging, and building dynamic applications that work with unknown object structures.

Updated on: 2026-03-15T23:19:00+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements