I have a class Unit where I define static Actions for a game
class Unit {
static Actions = {
Idle: 0,
Move: 1,
Attack: 2
}
}
I can then refer to this Actions by its name, instead of the value. So in my code I can do something like
if (soldier.action == Unit.Actions.Move) {
// Moving logic
}
However I noticed that when creating a dictionary, I cannot use this as keys, neither using the dot syntax or the [] syntax.
let myDict= {
0: "This is Action Idle" // Correct
Unit.Actions.Move: "This is Action Move", // Incorrect
Unit["Actions"]["Attack"]: "This is Action Attack" // Incorrect
}
Uncaught SyntaxError SyntaxError: Unexpected token '.'
Uncaught SyntaxError SyntaxError: Unexpected token '['
Of course I can use the values of each action (0, 1, 2) but this defeats the purpose of the static dictionary, which is to refer to the Actions by their names, making the code much more clear.
How can I use this static Actions as dictionary keys?