Created
December 22, 2023 13:06
-
-
Save ceving/1668cde377d446642e2c5a56acba2ca9 to your computer and use it in GitHub Desktop.
JavaScript Enum
This file contains 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
export class Enum | |
{ | |
static Item = class | |
{ | |
#name; | |
#enumeration; | |
constructor(name, enumeration) | |
{ | |
this.#name = name; | |
this.#enumeration = enumeration; | |
} | |
get name() { return this.#name; } | |
get enumeration() { return this.#enumeration; } | |
toString() { return this.name; } | |
} | |
#name; | |
constructor (name, item_names) | |
{ | |
this.#name = `${name}`; | |
for (const item_name of item_names) | |
{ | |
const item = new Enum.Item(`${item_name}`, this); | |
Object.defineProperty(this, item.name, | |
{ value: item, | |
enumerable: true, | |
writable: false }); | |
} | |
} | |
get name() { return this.#name; } | |
get items() { return Object.values(this); } | |
toString() { return `${this.name}(${Object.keys(this).join(',')})`; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment