4

For example, I have some class with some methods:

class SomeClass() {
    someMethod1() {
    }
    someMethod2() {
    }
}

Can I get all function names of this class instance?

Object.keys(new SomeClass()) 

gives me an empty array.

I want to get array like ['someMethod1', 'someMethod2'].

Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
Mort Rainey
  • 43
  • 1
  • 3

1 Answers1

12

You have to call Object.getOwnPropertyNames() on the prototype property of the class.

class Test {
  methodA() {}
  methodB() {}
}

console.log(Object.getOwnPropertyNames(Test.prototype))

Note that this also includes the constructor, but you can easily exclude it if you want:

class Test {
  methodA() {}
  methodB() {}
}

console.log(
  Object.getOwnPropertyNames(Test.prototype)
    .filter(x => x !== 'constructor')
)
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177