On gists
Enum - JS
JavaScript
enum.js
Raw
#
enum Direction {
Up,
Down,
Left,
Right
}
const Colors = Object.freeze({
RED: Symbol("red"),
BLUE: Symbol("blue"),
GREEN: Symbol("green")
});
/**
* Enum for common colors.
* @readonly
* @enum {{name: string, hex: string}}
*/
const Colors = Object.freeze({
RED: { name: "red", hex: "#f00" },
BLUE: { name: "blue", hex: "#00f" },
GREEN: { name: "green", hex: "#0f0" }
});
function createEnum(values) {
const enumObject = {};
for (const val of values) {
enumObject[val] = val;
}
return Object.freeze(enumObject);
}
// { Up: 'Up', Down: 'Down', Left: 'Left', Right: 'Right' }
createEnum(['Up', 'Down', 'Left', 'Right']);
class Direction {
static Up = new Direction('Up');
static Down = new Direction('Down');
static Left = new Direction('Left');
static Right = new Direction('Right');
constructor(name) {
this.name = name;
}
toString() {
return `Direction.${this.name}`;
}
}