从构造函数es6调用静态方法 [英] Calling static method from constructor es6

查看:217
本文介绍了从构造函数es6调用静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试从javascript中的构造函数调用静态方法时,它说该方法不存在。

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?

推荐答案

this.constructor.staticMethod()

可用于避免直接引用该类(对于类继承和粘贴代码特别有用。

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

这篇关于从构造函数es6调用静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆