Static variables in JavaScript

后端 未结 30 2272
别那么骄傲
别那么骄傲 2020-11-22 01:55

How can I create static variables in Javascript?

30条回答
  •  别那么骄傲
    2020-11-22 02:28

    I used the prototype and that way it worked:

    class Cat extends Anima {
      constructor() {
        super(Cat.COLLECTION_NAME);
      }
    }
    
    Cat.COLLECTION_NAME = "cats";
    

    or using an static getter:

    class Cat extends Anima {
      constructor() {
        super(Cat.COLLECTION_NAME);
      }
    
      static get COLLECTION_NAME() {
        return "cats"
      }
    }
    

提交回复
热议问题