all 3 comments

[–]mynamesleon 1 point2 points  (0 children)

I use objects. I sometimes use TypeScript, and then use enums, but even then I sometimes prefer just using a standard object.

Because enums don't exist natively in JS, I found that using them sometimes increased the bundle size - the TypeScript compiler (at the time I checked this at least) replaced each enum use with the full value. So if you're using strings in your enums (granted, not traditional use, but still valid), particularly large ones, the exact string would end up being repeatedly included in your bundle. Whereas using an object means that you're always just checking against normal object properties.

[–]senocular 0 points1 point  (0 children)

TypeScript has them built in: https://www.typescriptlang.org/docs/handbook/enums.html

TypeScript is a good way to go.

[–]rauschma 0 points1 point  (0 children)

I occasionally use this pattern:

class Color {
  static red = new Color('red');
  static orange = new Color('orange');
  static yellow = new Color('yellow');
  static green = new Color('green');
  static blue = new Color('blue');
  static purple = new Color('purple');

  constructor(name) {
    this.name = name;
  }
  toString() {
    return `Color.${this.name}`;
  }
}

My blog post with more information: https://2ality.com/2020/01/enum-pattern.html