❤️

ARCHAI

Undergraduate / Rookie

避免多条件并列

开发中有时会遇到多个条件,执行相同的语句,也就是多个||这种:

if (status === 'process' || status === 'wait' || status === 'fail') {
  doSomething()
}

这种写法语义性、可读性都不太好。可以通过switch caseincludes这种进行改造。

switch case
switch(status) {
  case 'process':
  case 'wait':
  case 'fail':
    doSomething()
}
includes
const enum = ['process', 'wait', 'fail']
if (enum.includes(status)) {
  doSomething()
Archai
Built with Hugo
Theme Stack designed by Jimmy