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.
Created
April 17, 2019 05:03
-
-
Save tail-call/5538dd37eddb9f4b1fb6105e1e707193 to your computer and use it in GitHub Desktop.
Enum idea (JS)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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