简体   繁体   English

Javascript 枚举和抽象工厂

[英]Javascript enums and abstract factory

Recently writing my frontend application i run into problems with enums最近在编写我的前端应用程序时遇到了枚举问题

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

then in another part of code i want to use that enum to execute specific action in abstract factory pattern然后在另一部分代码中,我想使用该枚举以抽象工厂模式执行特定操作

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

is there any nice way to consolidate actionList and myEnum without changing values of myEnum, so that i dont have to hardcode into actionList fooValue and BarValue?有没有什么好的方法可以在不更改myEnum值的情况下合并actionListmyEnum ,这样我就不必硬编码到 actionList fooValue 和 BarValue 中?

the answear i was looking for is:我正在寻找的答案是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM