is switch(true) {... valid javascript?(是 switch(true) {... 有效的 javascript 吗?)
问题描述
我最近遇到了一个代码,其中 switch 语句似乎与 switch 中的答案(布尔值)和 case 中的表达式相反.代码按预期运行良好,但我担心跨浏览器.它是有效的javascript吗?
I recently came across code where a switch statement seemed reversed with the answer (boolean) in the switch and the expressions in the case. The code ran fine as intended but I'm concerned about cross browser. Is it valid javascript?
switch(true) {
case (y < 20):
//
break;
case (y < 60):
//
break;
case (y < 130):
//
break;
}
推荐答案
这个片段非常好.这只是另一种表达方式:
This snippet is perfectly fine. It's just another way of expressing:
if (y < 20) {
// ...
} else if (y < 60) {
// ...
} else if ( y < 130) {
// ...
}
这篇关于是 switch(true) {... 有效的 javascript 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:是 switch(true) {... 有效的 javascript 吗?


基础教程推荐
- npm start 错误与 create-react-app 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01