Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling static method from constructor es6 [duplicate]

When I am trying to call a static method from constructor in javascript it says the method doesn't exist.

class TestClass {

    constructor(){
        this.staticMethod();
    }

    static staticMethod() {

    }
}

This works fine if i try to call a normal method from constructor. If static methods belongs to class instead of instance why it's not allowing them to call from constructor?

like image 304
Ashok Avatar asked May 03 '16 11:05

Ashok


2 Answers

this.constructor.staticMethod()

can be used to avoid referring to the class directly (particularly useful for class inheritance and pasted code).

like image 171
Estus Flask Avatar answered Oct 11 '22 19:10

Estus Flask


You have to call it like this:

TestClass.staticMethod()
like image 27
dlopez Avatar answered Oct 11 '22 19:10

dlopez