关于 js 中使用 switch (true) 和 if else

2020-12-22 10:18:18 +08:00
 Flands
switch (true) {
  case this.status === 0 && data.type === 1:
  case (this.status === 2 || this.status === 3) && data.status === 2:
    this.nextBtnIsOk = true
    break;

  default:
    break;
}

if (
  (this.status === 0 && data.type === 1) ||
  ((this.status === 2 || this.status === 3) && data.status === 2)
) {
  this.nextBtnIsOk = true;
}

投个票,大家习惯写哪种格式?两种都可以,看团队代码风格。
主要是 if else 的括号太多,看着有点累,哈哈。。

3242 次点击
所在节点    程序员
38 条回复
islxyqwe
2020-12-22 10:23:59 +08:00
this.nextBtnIsOk = (this.status === 0 && data.type === 1) ||
((this.status === 2 || this.status === 3) && data.status === 2)
Flands
2020-12-22 10:29:43 +08:00
@islxyqwe 还在开发的功能,有可能有其他判断和逻辑,不一定只有 `this.nextBtnIsOk = true` 这一行
Flands
2020-12-22 10:31:20 +08:00
这里只讨论这两种风格,不要在意逻辑~
3dwelcome
2020-12-22 10:32:24 +08:00
我还是第一次看到 switch 还能有这种用法,果然 JS 是万能的,涨姿势。
3dwelcome
2020-12-22 10:35:59 +08:00
就个人而言,我喜欢写 early return,就是 nextBtnIsOk = true 后就直接一句 return, 也没多余判断。
可能子函数会多一点点,但流程比较容易看懂,也没有那么多括号和 else 。
Bijiabo
2020-12-22 10:45:32 +08:00
我倾向写 switch(true)
BreadKiller
2020-12-22 10:48:56 +08:00
我也是第一次看到 switch 这种写法。
就可读性来说 switch 这种写法确实比下面这种一大串的括号好看。

但是我一般遇到这种很多条件的情况都会把各种条件分别定义:
let a = this.status === 0 && data.type === 1;
let b = (this.status === 2 || this.status === 3) && data.status === 2;
if (a || b) {
this.nextBtnIsOk = true;
}
faceRollingKB
2020-12-22 11:23:05 +08:00
我的写法一般是 if + return

if(this.status === 0 && data.type === 1){
return;
}
if((this.status === 2 || this.status === 3) && data.status === 2){
this.nextBtnIsOk = true
return;
}
...
enjoyCoding
2020-12-22 11:23:52 +08:00
if (condition1) {
...
return
}
if (condition2) {
...
return
}
对于 condition 比较长的
const isCondition = () => {}
if (isCondition()) {}
faceRollingKB
2020-12-22 11:24:30 +08:00
相对于其他写法我觉得要更灵活
EscYezi
2020-12-22 11:44:44 +08:00
如果是这两种的话选第一种,有时也用#9 的方式
wednesdayco
2020-12-22 11:45:07 +08:00
为了方便扩展和理解我大概会这么写
condition = {conditionName0:[0,1],conditionName1:[[2,3],2]};

const getConditionResult = (cdt)=>{
const cdt0=Array.isArray(cdt[0])?cdt[0].includes(this.status):cdt[0]===this.status;
const cdt1=Array.isArray(cdt[1])?cdt[1].includes(data.status):cdt[1]===data.status;
return cdt0&&cdt1;
}

if(getConditionResult(condition.conditionName0)||getConditionResult(condition.conditionName1)){
return xxx=true;
}
kyuuseiryuu
2020-12-22 11:49:05 +08:00
condition 很长的话应该把他写成函数,而不是搞这种取巧。
abelmakihara
2020-12-22 11:50:47 +08:00
要么就极简 直接 this.nextBtnIsOk = (this.status === 0 && data.type === 1) || ((this.status === 2 || this.status === 3) && data.status === 2)
特别复杂就两遍 switch 好了
根据不同 type 做不同处理
abelmakihara
2020-12-22 11:51:55 +08:00
switch(type){
case 1: xxx();break;
case 2: xxx2();break;
huichao
2020-12-22 11:54:41 +08:00
switch. 喜欢 switch 的写法,性能比 if else 高。
solobat
2020-12-22 11:57:54 +08:00
break 挺丑的
AoEiuV020
2020-12-22 12:03:12 +08:00
后者,
switch 这样写太丑了,
heyzoo
2020-12-22 12:13:02 +08:00
这让我想起了 switch case 正则判断。我更倾向于 switch
Biwood
2020-12-22 12:54:00 +08:00
看到过这种写法,但是我个人一直都没这么写过,代码毕竟是写给人看的,怎么直观怎么来。如果条件语句太长,要么换行,格式化代码,要么封装成函数。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/737737

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX