簡體   English   中英

Javascript 枚舉和抽象工廠

[英]Javascript enums and abstract factory

最近在編寫我的前端應用程序時遇到了枚舉問題

const myEnum = Object.freeze({
 fooKey: 'fooValue',
 barKey: 'barValue',
})

然后在另一部分代碼中,我想使用該枚舉以抽象工廠模式執行特定操作

fooAction(){
 //some-code
}
barAction(){
 //some-code
}
const actionList = {
 fooValue: fooAction,
 barValue: barAction
}
executeAction(enumValue){
 return actionList[enumValue]()
}

有沒有什么好的方法可以在不更改myEnum值的情況下合並actionListmyEnum ,這樣我就不必硬編碼到 actionList fooValue 和 BarValue 中?

我正在尋找的答案是:

const actionList = {
 [myEnum.fooKey]: fooAction,
 [myEnum.barKey]: barAction
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM