javascript - 在 Javascript 中使用 switch 语句处理多个 case 时,如何引用一个 case?

标签 javascript switch-statement

所以我有一些代码:

var Animal = 'Giraffe';
switch (Animal) {
  case 'Cow':
  case 'Giraffe':
  case 'Dog':
  case 'Pig':
    console.log('This animal will go on Noah\'s Ark.');
    break;
  case 'Dinosaur':
  default:
    console.log('This animal will not.');
}

理想情况下,我希望第一个 console.log 打印“This Giraffe will go on Noah's Ark”,或者任何变量 Animal 碰巧是什么。我想引用一下这个案例。但是,我不确定如何编写该代码。有人可以帮忙吗?

最佳答案

只需对 logAnimal 使用附加参数即可。

console.log('This', Animal, 'will go on Noah\'s Ark.');

关于javascript - 在 Javascript 中使用 switch 语句处理多个 case 时,如何引用一个 case?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42742855/

相关文章:

javascript - 替换动态大小的捕获组

javascript - SSR React应用程序的渲染过程和异步代码执行

javascript - jQuery:在同一个网站上运行 2 个不同的 jQuery 版本?

javascript - 获取 Web 服务器时间并将其显示在网页上的正确方法

c - 使用枚举时的高效 switch 语句

java - Switch Case 语句使用类级别变量

javascript - ESC 键关闭弹出窗体

c - switch 和 if else 语句的区别

Swift 3.0 多个选择器 View 无法识别数据源变量

c++ - 替代长 switch 语句