How to list all methods of an object in JavaScript
We can use the Object.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 using typeof on 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 we 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') >
getMethods("") getMethods(new String('test')) getMethods(<>) getMethods(Date.prototype)
Javascript find all functions
The following three functions are very useful on inspecting methods and properties in any Javascrip object.
Github code: https://github.com/nsclass/describe-javascript-object-methods/blob/master/list-object-methods.js
const getArgs = func => < // First match everything inside the function argument parens. let matchedItems = func.toString().match(/function\s.*?\(([^)]*)\)/) if (matchedItems && matchedItems.length >0) < let args = matchedItems[1].trim() if (args.length >0) < // Split the arguments string into an array comma delimited. return args.split(',').map( arg =>< // Ensure no inline comments are parsed and trim the whitespace. return arg.replace(/\/\*.*\*\//, '').trim() >) > > return "no args" > const getMethods = (obj) => < return Object.entries(obj) .filter((Javascript find all functions) =>typeof objJavascript find all functions === 'function') .map((Javascript find all functions) => `$: $`) .sort((x, y) => < let strX = x.toString() let strY = y.toString() return strX.localeCompare(strY) >) > const getProperties = obj => < return Object.getOwnPropertyNames(obj) .filter(item =>typeof obj[item] !== 'function') >
For instance, we can list all available functions and each function’s arguemnts in fs node module.
const fs = require('fs) getMethods(fs)
[ '_debugEnd: no args', '_debugProcess: no args', '_fatalException: no args', '_getActiveHandles: no args', '_getActiveRequests: no args', '_kill: no args', '_linkedBinding: module', '_rawDebug: no args', '_startProfilerIdleNotifier: no args', '_stopProfilerIdleNotifier: no args', '_tickCallback: no args', 'abort: no args', 'activateUvLoop: no args', 'assert: . args', 'atomBinding: name', 'binding: module', 'chdir: . args', 'cpuUsage: prevValue', 'crash: no args', 'cwd: no args', 'dlopen: no args', 'emitWarning: no args', 'exit: no args', 'getCPUUsage: no args', 'getCreationTime: no args', 'getegid: no args', 'geteuid: no args', 'getgid: no args', 'getgroups: no args', 'getHeapStatistics: no args', 'getIOCounters: no args', 'getRenderProcessPreferences: no args', 'getSystemMemoryInfo: no args', 'getuid: no args', 'hang: no args', 'hasUncaughtExceptionCaptureCallback: no args', 'hrtime: time', 'initgroups: . args', 'kill: no args', 'log: no args', 'memoryUsage: no args', 'NativeModule: id', 'nextTick: no args', 'openStdin: no args', 'reallyExit: no args', 'setegid: . args', 'seteuid: . args', 'setFdLimit: no args', 'setgid: . args', 'setgroups: . args', 'setuid: . args', 'setUncaughtExceptionCaptureCallback: no args', 'takeHeapSnapshot: no args', 'umask: . args', 'uptime: no args' ]