Skip to content Skip to sidebar Skip to footer

ES6 Static Method Referring To Self?

I have two classe; Repository and UserRepository. I want to define a static method in Repository that at runtime calls a static function in UserRepository. Is there any clean way t

Solution 1:

Now it makes sense that the above doesn't work, as this probably referes to an instance, and I have no instances in this case.

No, how would this refer to an instance, as you say you don't have any?

No, static methods are just functions like any other methods as well, and this refers to whatever they were invoked on. In UserRepository.printModel();, this will refer to UserRepository. And you can just use this.model() to call the static .model() method of that class.


Post a Comment for "ES6 Static Method Referring To Self?"