Last active
May 31, 2024 07:52
-
-
Save Kcko/59fb85a1441b693873d1944ef638400a to your computer and use it in GitHub Desktop.
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
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