JavaScript, Typescript switch statement: way to run same code for two cases?(JavaScript,Typescript switch 语句:两种情况下运行相同代码的方式?)
问题描述
有没有一种方法可以在不复制和粘贴的情况下将两个不同的 case 值分配给同一个代码块?比如68以下和40应该执行相同的代码,而30是不相关的.
Is there a way to assign two different case values to the same block of code without copy and pasting? For example, below 68 and 40 should execute the same code, while 30 is not related.
case 68:
//Do something
break;
case 40:
//Do the same thing
break;
case 30:
//Do something different
break;
认为这样的事情应该有效(即使它显然没有)是不正确的吗?
Is it incorrect to think something like this should work (even though it obviously doesn't)?
case 68 || 40:
//Do something
break;
case 30:
//Do something else
break;
推荐答案
把它们一个接一个地放在一起就行了
Just put them right after each other without a break
switch (myVar) {
case 68:
case 40:
// Do stuff
break;
case 30:
// Do stuff
break;
}
这篇关于JavaScript,Typescript switch 语句:两种情况下运行相同代码的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JavaScript,Typescript switch 语句:两种情况下运行相同代码的方式?


- 400或500级别的HTTP响应 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01