![](/img/trans.png)
[英]Behavior of a JavaScript switch statement with the .attr() method
[英]Javascript switch statement with includes method
好吧,有點奇怪,要么我在這里做的事情比我想象的要復雜一點,要么我的大腦不工作,但我正在嘗試使用 switch 語句來檢查字符串是否包含某些內容,然后運行一段代碼...
const msg = 'hash-test'
switch (msg) {
case msg.includes('foo'):
// do something
break;
case msg.includes('hash-'):
// do something else
break;
}
試圖弄清楚為什么我的msg.includes('hash-')
案例語句沒有運行,因為我的字符串確實包含hash
有什么想法嗎?
switch(msg)
將字符串msg
與 boolean case msg.includes('foo')
進行比較,這絕不是嚴格相等的。
所以:不要在這里使用switch
語句!
const msg = 'hash-test'
if (msg.includes('foo') {
// do something
} else if (msg.includes('hash-')) {
// do something else
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.