Skip to content

Instantly share code, notes, and snippets.

@Kcko
Last active May 31, 2024 07:52
Show Gist options
  • Save Kcko/59fb85a1441b693873d1944ef638400a to your computer and use it in GitHub Desktop.
Save Kcko/59fb85a1441b693873d1944ef638400a to your computer and use it in GitHub Desktop.
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}`;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment