Access to static properties via this.constructor in typescript

前端 未结 6 2273
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-18 13:49

I want to write es6 class:

class SomeClass {
    static prop = 123

    method() {
    }
}

How to get access to static prop from <

6条回答
  •  余生分开走
    2021-02-18 14:37

    Usually the simple way is:

    class SomeClass {
        static prop = 123
    
        method() {
            console.log(SomeClass.prop)  //> 123
        }
    }
    

    Note that if you use this, subclasses of SomeClass will access the SomeClass.prop directly rather than SomeSubClass.prop. Use basarat's method if you want subclasses to access their own static properties of the same name.

提交回复
热议问题