enum 按鍵使用實例
- 用enum列舉常用modules的參數類別
- 權限表弄enum
- 產品如果需要快捷鍵支援的話,也會做這種類似MOUSE、方向鍵、keydown之類操作(策略模式+enum)
ts
enum Key {
UP = "ArrowUp",
DOWN = "ArrowDown",
LEFT = "ArrowLeft",
RIGHT = "ArrowRight",
Q = "Q",
W = "W",
E = "E",
R = "R",
}
const handleUp = () => {
console.log("Move up");
};
const handleDown = () => {
console.log("Move down");
};
const handleLeft = () => {
console.log("Move left");
};
const keyStrategies: { [key in Key]: () => void } = {
[Key.UP]: handleUp,
[Key.DOWN]: handleDown,
[Key.LEFT]: handleLeft,
[Key.RIGHT]: handleRight,
[Key.Q]: handleQ,
[Key.W]: handleW,
[Key.E]: handleE,
[Key.R]: handleR,
};
const handleKeydown = (event: KeyboardEvent) => {
const key = event.key as Key;
if (key in keyStrategies) {
keyStrategies[key]();
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
- 後端比較常用來列舉狀態或錯誤類型,例如某個資料的 type 種類、某個 service 會 throw 的錯誤類型等等。 前端我後來比較常用 defineConstants,具體作法
- 狀態控管用 ENUM
- 系統內應該會有不少需要判別字串是否符合某些POOL的條件,這就是enum的用意