枚举类型的枚举值默认是1, 2, 3
enum Direction {
up,
down,
left,
right,
}
console.log(Direction.down); // 1
console.log(Direction[0]); // 这个值反解回来就是"up"这个字符串
自定义枚举值
enum Direction {
up = "a",
down = "b",
left = "c",
right = "d",
}
console.log(Direction.down); // a
console.log(Direction["a"]); // up
console.log(Direction["a"] === "up"); // true
常量枚举
常量枚举可以提升性能, 但是不能反解
const enum Direction {
up,
down,
left,
right,
}
console.log(Direction.down);
console.log(Direction[1]);