How do I make a “public static field” in an ES6 class?

后端 未结 5 2031
滥情空心
滥情空心 2020-11-27 14:13

I\'m making a Javascript class and I\'d like to have a public static field like in Java. This is the relevant code:

export default class Agent {
    CIRCLE:          


        
5条回答
  •  再見小時候
    2020-11-27 15:12

    In current drafts of ECMAScript 6 (as of February 2015), all class properties must be methods, not values (note in ECMAScript a "property" is similar in concept to an OOP field, except the field value must be a Function object, not any other value such as a Number or Object).

    You can still specify these using traditional ECMAScript constructor property specifiers:

     class Agent {
     }
     Agent.CIRCLE = 1;
     Agent.SQUARE = 2;
     ...
    

提交回复
热议问题