Published
- 1 min read
javascript get all methods of object
The solution for this is noted below
javascript get all methods of object
Solution
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)
Try other methods by searching on the site. That is if this doesn’t work