const AnimeState = Object.freeze({
Idle = 1,
Walking=2,
Running=3,
Attacking = 4,
Dead = 0,
Uninitialized: -1
});
this.state = AnimeState.Uninitialized
Trying to get the name of the state, console.log(AnimeState[this.state])
--- this doesn't work however, maybe because of me using the Object.freeze()
method here. Is there a way to get both worlds?
Desire:
console.log(AnimeState[this.state])
if state === AnimeState.Uninitialized
, give me Uninitialized
as string
.
I followed this link: How to get names of enum entries? and there, they pointed:
enum colors { red, green, blue };
Will be converted essentially to this:
var colors = { red: 0, green: 1, blue: 2,
[0]: "red", [1]: "green", [2]: "blue" }
Because of this, the following will be true:
colors.red === 0
colors[colors.red] === "red"
colors["red"] === 0
This creates a easy way to get the name of an enumerated as follows:
var color: colors = colors.red;
console.log("The color selected is " + colors[color]);