Can a private method of a JavaScript class be accessed dynamically?

Viewed 50

Suppose I have a class in Javascript as follows:

class T {
   #p() {
   }
   a(s) {
     this['#'+s]();
   }
}
t = new T();

My intention is that when I call t.a('p'), #p would be called in return. However, this doesn't work, and a debug shows that this['#p'] is undefined within a. Public methods work fine using this method, so I was wondering if there is anyway to invoke a private method within a class method based on its name dynamically.

Thanks!

0 Answers
Related