在执行其他类方法之前是否可以使用某些类方法?

时间:2017-09-22 14:17:50

标签: javascript

例如我有JS(ES6)类

class ClassA {

    constructor() {        
        /*  some constructor body */      
    }    

    initMetond() {                
        /*  some method body */  
    }

    method1() {
        /*  some method1 body */
    }

    method2() {
        /*  some method2 body */
    }

    method3() {
        /*  some method3 body */
    }
}

我需要在任何其他类方法之前执行initMethod()(但不在构造函数中)。目前我按照以下方式做到了

class ClassA {

    constructor() {        
        /*  some constructor body */      
    }    

    initMetond() {                
        /*  some method body */  
    }

    method1() {
        this.initMetond();
        /*  some method1 body */
    }

    method2() {
        this.initMetond();
        /*  some method2 body */
    }

    method3() {
        this.initMetond();
        /*  some method3 body */
    }
}

但是,我希望,它可以有更好的实现。 请指教?

0 个答案:

没有答案
相关问题