博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JavaScript切换条件
阅读量:2509 次
发布时间:2019-05-11

本文共 1589 字,大约阅读时间需要 5 分钟。

An if/else statement is great when you have a few options to choose.

当您有一些选择时, if/else语句非常有用。

When they are too many however it might be overkill. Your code will look too complex.

但是,当它们太多时,可能就太过分了。 您的代码看起来太复杂了。

In this case you might want to use a switch conditional:

在这种情况下,您可能需要使用条件switch

switch(
) { //cases}

based on the result of the expression, JavaScript will trigger one specific case you define:

根据表达式的结果,JavaScript将触发您定义的一种特定情况:

const a = 2switch(a) {  case 1:    //handle case a is 1    break  case 2:    //handle case a is 2    break  case 3:    //handle case a is 3    break}

You must add a break statement at the bottom of each case, otherwise JavaScript will also execute the code in the next case (and sometimes this is useful, but beware of bugs). When used inside a function, if the switch defines a return value, instead of using break you can just use return:

您必须在每种情况的底部添加一个break语句,否则JavaScript也会在下一种情况下执行代码(有时这是有用的,但是要注意错误)。 在函数内部使用时,如果开关定义了返回值,则可以使用return来代替使用break

const doSomething = (a) => {  switch(a) {    case 1:      //handle case a is 1      return 'handled 1'    case 2:      //handle case a is 2      return 'handled 2'    case 3:      //handle case a is 3      return 'handled 3'  }}

You can provide a default special case, which is called when no case handles the result of the expression:

您可以提供default特殊情况,当没有大小写处理表达式的结果时调用该特殊情况:

const a = 2switch(a) {  case 1:    //handle case a is 1    break  case 2:    //handle case a is 2    break  case 3:    //handle case a is 3    break  default:    //handle all other cases    break}

翻译自:

转载地址:http://lvqgb.baihongyu.com/

你可能感兴趣的文章
Python 基于python编写一些算法程序等
查看>>
Python 一键commit文件、目录到SVN服务器
查看>>
毕业5年决定你的命运 ----值得所有不甘平庸的人看看
查看>>
基于Python的接口自动化-01
查看>>
前台返回json数据的常用方式+常用的AJAX请求后台数据方式
查看>>
spring boot下MultipartHttpServletRequest如何提高上传文件大小的默认值
查看>>
css继承和边框圆角 及 写三角形
查看>>
编译opencv有关cuda的代码
查看>>
spring quartz job autowired 出错 null pointer
查看>>
openfire 安装部署
查看>>
数据库查询某一字段为空的数据
查看>>
GridView使用CommandField删除列实现删除时提示确认框
查看>>
23. CTF综合靶机渗透(十六)
查看>>
【caffe】train_lenet.sh在windows下的解决方案
查看>>
【机器学习】--回归问题的数值优化
查看>>
用C# 连接 hadoop,hive,hbase的一些代码
查看>>
Linux挂载U盘
查看>>
linux下LCD(framebuffer)驱动分析...
查看>>
FZu Problem 2233 ~APTX4869 (并查集 + sort)
查看>>
php程序面试题
查看>>