Search code examples
javascript

How can I call a private method from a public method in javascript?


class myClass
{
   myPublicMethod(e)
   {
      console.log(this);
      myPrivateMethod(); //does not work
   }

   #myPrivateMethod()
   {
      console.log('in myPrivateMethod');
   }
}

I've confirmed that this is myClass via console.log. I get the error myPrivateMethod is not defined. If I try this.myPrivateMethod(); then I get the error myPrivateMethod is not a function. The only thing that works is removing the # from myPrivateMethod making it public and calling it via this.my(formerly)PrivateMethod();

Is it possible to call this function while keeping it private?


Solution

  • I think the answer may be as simple as including the # when you call the method, as in this.#myPrivateMethod(). The docs for this are here.