【发布时间】:2022-12-19 15:28:02
【问题描述】:
我有以下代码:
ctrl = {
dataHolder: {
hide: _ => {
this.dataHolderTarget.style.display = 'none'
},
show: _ => {
this.dataHolderTarget.style.display = 'block'
}
},
dataBtnHolder: {
hide: _ => {
this.dataBtnHolderTarget.style.display = 'none'
},
show: _ => {
this.dataBtnHolderTarget.style.display = 'block'
}
},
playerOneBtn: {
hide: _ => {
this.playerOneBtnTarget.style.display = 'none'
},
show: _ => {
this.playerOneBtnTarget.style.display = 'block'
},
load: res=>{
this.dataset.res=res
}
},
playerTwoBtn: {
hide: _ => {
this.playerTwoBtnTarget.style.display = 'none'
},
show: _ => {
this.playerTwoBtnTarget.style.display = 'block'
},
load: res=>{
this.dataset.res=res
}
}
}
我怎样才能不重复那些代码块?正如我对 DRY 的了解,这段代码看起来有些难看。如您所见,hide 和show 无处不在。最后 2 个元素具有 load 功能。
【问题讨论】:
-
this.playerTwoBtnTarget和其他元素是如何分配的?ctrl是怎么称呼的?此外,这可能更适合Code Review
标签: javascript