关于 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 的括号太多,看着有点累,哈哈。。

3267 次点击
所在节点    程序员
38 条回复
love
2020-12-22 12:58:54 +08:00
这种 switch 写法是什么鬼?从来没见过,而且感觉很容易出错。

switch (true) { case true: console.log("FUCK"); }
这的确可以


switch (true) { case 1: console.log("FUCK"); }
这就不行了吧,你得确保条件都返回 true 别的 true 值都不行,这太容易出错了,比如一个函数返回值你不能确定就是 true,你得用 case Boolean(isSomething())包一层
weixiangzhe
2020-12-22 13:00:34 +08:00
还能这样玩啊,涨见识了,话说这个 do-expression 也挺好的
https://babeljs.io/docs/en/babel-plugin-proposal-do-expressions
dartabe
2020-12-22 13:01:43 +08:00
楼上的 if + early return 比较好

反正我感觉只有 if 没有 else 的代码比较好读
zmNv0
2020-12-22 14:03:13 +08:00
若单变量,使用 switch
若多变量,使用 if return
xingchong
2020-12-22 14:17:23 +08:00
js 成天被喷简单,是个人都会用,看楼上回复竟然这么多第一次见到 switch ???
都是后端程序猿吗?
用哪种方式,主要看业务逻辑,比如判断订单状态,每个状态对应一种逻辑的话,switch 很直观,最后再加一个 default,这种用 ifelse 就看着有点啰嗦。
GuuJiang
2020-12-22 14:20:30 +08:00
@xingchong 这是我见过后端被黑的最惨的一次,楼上说的不是第一次见 switch,而是第一次见 switch(true)
xingchong
2020-12-22 14:28:11 +08:00
@GuuJiang soga,我说呢,看错了,哈哈哈
chenyu0532
2020-12-22 14:53:47 +08:00
我绝大多数情况下使用 if else,莫名的看 swtich 不顺眼
leeton
2020-12-22 14:56:26 +08:00
居然能用 switch 。
Lemeng
2020-12-22 15:01:30 +08:00
喜欢 if
DOLLOR
2020-12-22 15:54:41 +08:00
不喜欢 switch-case 的可以直接用 label 语句,

let nextBtnIsOk = true
label:{
if(this.status === 0 && data.type === 1) break label;
if((this.status === 2 || this.status === 3) && data.status === 2) break label;
nextBtnIsOk = false;
}
this.nextBtnIsOk = nextBtnIsOk;
forgottencoast
2020-12-22 15:56:00 +08:00
考虑代码可读性,if 。
acmore
2020-12-22 16:06:00 +08:00
如果 If 语句都要写成下边这种形式我选 Switch,读起来太累,然而你不必非要这么写 If 语句,7 楼的写法就舒服很多了。
DOLLOR
2020-12-22 16:27:59 +08:00
@BreadKiller
你的这写法会导致每次执行都要计算每个条件的值。改进一下可以使得前面的条件满足后不再计算后面的条件值。

let willSet;
willSet = willSet || this.status === 0 && data.type === 1;
willSet = willSet || (this.status === 2 || this.status === 3) && data.status === 2;
willSet = willSet || xxxxx;
willSet = willSet || yyyyy;
willSet = willSet || zzzzz;
// ...

if (willSet) {
this.nextBtnIsOk = true;
}
Reapper
2020-12-22 16:29:25 +08:00
if + return
ciddechan
2020-12-23 09:06:52 +08:00
let a = '3';
switch (a) {
case 3:
console.log(false)
break;
case '3':
console.log(true)
break;
}

console.log(a==3)
console.log(a=='3')
console.log(a===3)
console.log(a==='3')

switch 约等于 ===
jifengg
2020-12-23 13:53:46 +08:00
也是第一次知道 switch 还能这么玩。
cnelf
2021-03-14 23:43:14 +08:00
switch 的可读性确实要强一些,但是感觉不太符合 switch 在设计上的语义。

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

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

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

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

© 2021 V2EX