Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading