class A { launch() { let step = this.step++; let coords = this.stops[0][step]; Tutor.moveRect(coords); Tutor.next(); } static next() { let next = document.querySelectorAll(".next")[0]; next.addEventListener("click", launch); // * } } 

The next () method internally calls the launch () method (should call), but this does not happen, and I get an error:

Uncaught ReferenceError: launch is not defined

Tried to do it too?

 static next() { let next = document.querySelectorAll(".next")[0]; next.addEventListener("click", function() { launch(); }); } 

But it does not work.

And I tried it like this:

 class A { static launch() { let step = this.step++; let coords = this.stops[0][step]; Tutor.moveRect(coords); Tutor.next(); } static next() { let next = document.querySelectorAll(".next")[0]; next.addEventListener("click", function() { A.launch(); }); } } 

But it does not suit me.

How to get around this behavior?


About possible duplicates.

Here , for example, a different syntax is used. So the answer to that question does not suit me.

  • 2
    From a static method, you can only call a static method (your last example). If you want to call an instance method, you need the class instance itself. If this does not suit you (I don’t know why), you have problems with the architecture of the application. OOP has nothing to do with it - Dmitriy Simushev
  • @DmitriySimushev, the fact is that the launch () method is used to launch an application, respectively, it is launched from an object instance and cannot be static according to its logic. But thanks for the tip, I think it’s worth writing back, because this is the answer. - smellyshovel
  • one
    @smellyshovel so create a new object (new A()).launch() - etki

0