Skip to content

Instantly share code, notes, and snippets.

@ceving
Created December 22, 2023 13:06
Show Gist options
  • Save ceving/1668cde377d446642e2c5a56acba2ca9 to your computer and use it in GitHub Desktop.
Save ceving/1668cde377d446642e2c5a56acba2ca9 to your computer and use it in GitHub Desktop.
JavaScript Enum
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