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']
.
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')
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With