Skip to content

Instantly share code, notes, and snippets.

@tail-call
Created April 17, 2019 05:03
Show Gist options
  • Save tail-call/5538dd37eddb9f4b1fb6105e1e707193 to your computer and use it in GitHub Desktop.
Save tail-call/5538dd37eddb9f4b1fb6105e1e707193 to your computer and use it in GitHub Desktop.
Enum idea (JS)

I wanted to have an enum class that would 1) allow me to create enum with arbitrary number of members and 2) allow to test any object to be a member of particular enum.

class Enum {
static create(...members) {
class DerivedEnum extends Enum {
static contains(member) {
return member instanceof DerivedEnum;
}
}
for (let member of members) {
DerivedEnum[member] = new DerivedEnum();
}
return DerivedEnum;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment