维护项目代码
发现别人的写法
this.triggerEvent('myevent1', {
classid: index == 0 ? this.data.classid == 2 ? 1 : this.data.classid == 1 ? 2 : '' : this.data.classid == 2 ? 3 : this.data.classid == 1 ? 4 : '',
index: this.data.index1,
})
有没有哪位小伙伴解读下判断逻辑是怎样的?
这嵌套的层数够深的,写个switch不香吗
if(index == 0)
if(this.data.classid == 2 )
return 1
else
if(this.data.classid == 1)
return 2
else
return ''
else
if(this.data.classid == 2)
return 3
else
if(this.data.classid == 1)
return 4
else
return ''
代码的可读性才是最重要的,不建议一长串三元判断,可读性较差
if (index == 0) {
if (this.data.classid == 2) {
return 1;
} else if (this.data.classid == 1) {
return 2;
} else {
return '';
}
} else {
if (this.data.classid == 2) {
return 3;
} else if (this.data.classid == 1) {
return 4;
} else {
return '';
}
}