Static Constructor in Javascript ES6

后端 未结 3 1529
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-20 13:36

In ES6, I can create static methods like below. But I need to define a static constructor but no success. I need something that runs only once when the class is loaded. I Is

3条回答
  •  太阳男子
    2020-12-20 14:05

    I need something that runs only once when the class is loaded.

    You shouldn't be using classes if you just use them as a bag of methods. Use an object instead. However, it's still possible to run such code. Just put it before or after the class definition.

    console.log('before class is created')
    
    class Foo {}
    
    console.log('after class was created');
    

提交回复
热议问题