-2

I have encountered with something like this:

class Example {
  // ...

  async #privateMethod() {
    return true
  }

  static staticMethod() {
    try {
       // can't access #privateMethod
      this.#privateMethod()
    } catch (error) {
      console.log('error', error);
    }
  }
}

I want to call a private method within the static method, but I can't access the private method for some reason. What am I doing wrong, please?

frostack
  • 1
  • 1
  • 2
    The private method is an **instance** method. A static method has no instance to work with -- `this` is the constructor. Which instance did you expect to work with? – trincot Apr 15 '23 at 07:04
  • 2
    It would help if you would give a more concrete example, with an explanation of what you want to achieve with the class, with the private method, and with the static method. As it is written now, it is not clear what you want to achieve. It is normal you cannot access instance methods on `this`, when `this` is not an instance. – trincot Apr 15 '23 at 07:08
  • Oh, I didn't know that's the case regarding the instance etc. So, what should I do if I want for example to create a static method "updateUser" and within it I want to call a private method for checking if that user exist or something, like "#isUserExist"? I mean, I don't want to make an instance for a User class, because I create my user like that. Or I should create a user with my instance? – frostack Apr 15 '23 at 07:16
  • 2
    Looks like your `#isUserExists` shouldn't be an instance method. I don't see a good reason why it should be private either, so also make it a static method. An instance method is only useful if you have some "current" state to work with, like a specific user. But when you want to check if an identifier corresponds with a user, you don't have a user yet, so there is no state to work with, and so it should be a static method. – trincot Apr 15 '23 at 07:17
  • Oh, wow... thanks for that explanations. But what if I want to obscure the method, for example, and then use it within the static method? I can't do that? I mean, that's one of the principles, is it not? to kind of limit access for things that you don't want the outside world to interact with. What if that's the case? – frostack Apr 15 '23 at 07:26
  • There is nothing stopping you to just declare the private method as static. See answer I posted. – trincot Apr 15 '23 at 07:51

2 Answers2

2

That private method is an instance method, meaning you would need this to be an instance of Example to do this.#privateMethod.

When running a static method, you don't have an instance. Instead, this refers to the constructor (the "class").

This may be an indication that #privateMethod shouldn't be an instance method after all, as it could do its job without one.

If this is the case, then just make that private method also static:

class Example {
  // ...

  static async #privateMethod() {
    console.log("running private static method");
    return true;
  }

  static staticMethod() {
    try {
      this.#privateMethod()
    } catch (error) {
      console.log('error', error);
    }
  }
}

Example.staticMethod();
trincot
  • 317,000
  • 35
  • 244
  • 286
0

In a static method there is nothing like this. This is the instance of an class created with

myClassInstance = new Classname(); 

And then you call the methods with

MyClassInstance.myMethod();

In this method you can then use this as a pointer to the myClassInstance.

A static method is called without such an instance so you have no this and so you can’t call non static methods.

Thallius
  • 2,482
  • 2
  • 18
  • 36
  • But this is not practical... I need to use try-catch block within my static method. – frostack Apr 15 '23 at 07:00
  • @frostack The `try..catch` has absolutely nothing to do with the problem. It’s about trying to access an instance method from not-an-instance. – deceze Apr 15 '23 at 07:11