Static variables in JavaScript

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

How can I create static variables in Javascript?

30条回答
  •  孤独总比滥情好
    2020-11-22 02:31

    If you are using the new class syntax then you can now do the following:

        class MyClass {
          static get myStaticVariable() {
            return "some static variable";
          }
        }
    
        console.log(MyClass.myStaticVariable);
    
        aMyClass = new MyClass();
        console.log(aMyClass.myStaticVariable, "is undefined");

    This effectively creates a static variable in JavaScript.

提交回复
热议问题