Enums in Javascript with ES6

前端 未结 15 1900
青春惊慌失措
青春惊慌失措 2020-12-12 10:42

I\'m rebuilding an old Java project in Javascript, and realized that there\'s no good way to do enums in JS.

The best I can come up with is:

const C         


        
15条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-12 11:34

    you can also use es6-enum package (https://www.npmjs.com/package/es6-enum). It's very easy to use. See the example below:

    import Enum from "es6-enum";
    const Colors = Enum("red", "blue", "green");
    Colors.red; // Symbol(red)
    

提交回复
热议问题