Hacker News new | ask | show | jobs
by ahoge 4018 days ago
> does JS have enums?

No, but TypeScript and Dart do.

Enums don't really work without types.

But using Symbols for fake enums is probably a good idea.

    class Enum {
      constructor(...props) {
        props.forEach(p => this[p] = Symbol(p));
      }
    }
    const dir = Object.freeze(new Enum(...'NESW'));
    console.log(dir.E === dir.E); // true
    console.log(dir.E === dir.N); // false
Slightly awkward, but this might be about as good as it gets.
1 comments

I should have used Object.freeze(this) in the c'tor.