skip to Main Content
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?

2

Answers


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

    Login or Signup to reply.
  2. It works as the following example:

    class myClass
    {
       myPublicMethod(e)
       {
          console.log(this);
          this.#myPrivateMethod();
       }
    
       #myPrivateMethod()
       {
          console.log('in myPrivateMethod');
       }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search