做满屏网站的尺寸,六安城市网电话是多少,江门关键词排名工具,域名解析器前言
你是否写过这样代码: ...if (status 1 || status 4 || status 6)...代码场景是这样的#xff0c;记录有多个状态#xff0c;当状态等于1,4,6时要做相同的逻辑。今天我们就分享一下如何简化写法#xff0c;让代码更好看#xff0c;更优雅。
使用 switch 语句
...…前言
你是否写过这样代码: ...if (status 1 || status 4 || status 6)...代码场景是这样的记录有多个状态当状态等于1,4,6时要做相同的逻辑。今天我们就分享一下如何简化写法让代码更好看更优雅。
使用 switch 语句
...switch (status) {case 1:case 4:case 6:return true;default:return false;}
...使用 Array.prototype.includes(推荐)
...// 或条件if([1, 4, 6].includes(status))
...使用 Set 对象
...new Set([1, 4, 6]).has(status)
...使用正则表达式(推荐)
.../^1|4|6$/.test(status)
...