在JavaScript中切换语句多个案例

我需要JavaScript中的switch语句中的多个情况,例如:

switch (varName) { case "afshin", "saeed", "larry": alert('Hey'); break; default: alert('Default case'); break; } 

我怎样才能做到这一点? 如果在JavaScript中没有办法做这样的事情,我想知道另一个也遵循DRY概念的解决scheme。

使用switch语句的fall-throughfunction。 一个匹配的情况将会运行,直到find一个break (或switch语句的结尾),所以你可以这样写:

 switch (varName) { case "afshin": case "saeed": case "larry": alert('Hey'); break; default: alert('Default case'); } 

这是完全避免switch语句的另一种方法:

 var cases = { afshin: function() { alert('hey'); }, _default: function() { alert('default'); } }; cases.larry = cases.saeed = cases.afshin; cases[ varName ] ? cases[ varName ]() : cases._default(); 

这在常规的JavaScript中工作

 function theTest(val) { var answer = ""; switch( val ) { case 1: case 2: case 3: answer = "Low"; break; case 4: case 5: case 6: answer = "Mid"; break; case 7: case 8: case 9: answer = "High"; break; default: answer = "Massive or Tiny?"; } return answer; } theTest(9); 

干杯。

在Js中分配多个开关的情况我们必须定义different case without break如下所示:

  <script type="text/javascript"> function checkHere(varName){ switch (varName) { case "saeed": case "larry": case "afshin": alert('Hey'); break; case "ss": alert('ss'); break; default: alert('Default case'); break; } } </script> 

请参阅示例点击链接

如果你使用ES6,你可以这样做:

 if (['afshin', 'saeed', 'larry'].includes(varName)) { alert('Hey'); } else { alert('Default case'); } 

或者对于早期版本的JavaScript,您可以这样做:

 if (['afshin', 'saeed', 'larry'].indexOf(varName) !== -1) { alert('Hey'); } else { alert('Default case'); } 

请注意,这在旧版IE浏览器中不起作用,但是您可以很容易地修补它们。 查看问题确定string是否在JavaScript列表中的更多信息。

你可以使用' in '运算符…
依赖于对象/散列调用…
所以它的JavaScript一样快可以…

 // assuming you have defined functions f(), g(a) and h(a,b) // somewhere in your code // you can define them inside the object but... // the code becomes hard to read, I prefer this way o = { f1:f, f2:g, f3:h }; // if you use "STATIC" code can do: o['f3']( p1, p2 ) // if your code is someway "DYNAMIC", to prevent false invocations // m brings the function/method to be invoked (f1, f2, f3) // and you can rely on arguments[] to solve any parameter problems if ( m in o ) o[m]() 

享受ZEE

在节点中,似乎允许你这样做:

 data = "10"; switch(data){ case "1": case "2": case "3": //put multiple cases on the same line to save vertical space. console.log("small"); break; case "10": case "11": case "12": console.log("large"); break; default: console.log("strange"); break; } 

在某些情况下,这使得代码更加紧凑。

 switch (myVariable) { case "A": case "B": case "C": // Do something break; case "D": case "E": // Do something else break; default: // Default case break; } 

在这个例子中,如果myVariable的值是A,B或C,它将在“C”情况下执行代码。

这取决于。 开关只评估一次。 一旦匹配,所有后续的病例报告,直到“rest”火灾,无论如何说。

 var onlyMen = true; var onlyWomen = false; var onlyAdults = false; (function(){ switch (true){ case onlyMen: console.log ('onlymen'); case onlyWomen: console.log ('onlyWomen'); case onlyAdults: console.log ('onlyAdults'); break; default: console.log('default'); } })(); // returns onlymen onlywomen onlyadults 
 <script src="firebug-lite-debug.html"></script> 
 <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Example1</title> <link rel="stylesheet" href="css/style.css" > <script src="js/jquery-1.11.3.min.js" type="text/javascript"></script> <script> function display_case(){ var num = document.getElementById('number').value; switch(num){ case (num = "1"): document.getElementById("result").innerHTML = "You select day Sunday"; break; case (num = "2"): document.getElementById("result").innerHTML = "You select day Monday"; break; case (num = "3"): document.getElementById("result").innerHTML = "You select day Tuesday"; break; case (num = "4"): document.getElementById("result").innerHTML = "You select day Wednesday"; break; case (num = "5"): document.getElementById("result").innerHTML = "You select day Thusday"; break; case (num = "6"): document.getElementById("result").innerHTML = "You select day Friday"; break; case (num = "7"): document.getElementById("result").innerHTML = "You select day Saturday"; break; default: document.getElementById("result").innerHTML = "You select day Invalid Weekday"; break } } </script> </head> <body> <center> <div id="error"></div> <center> <h2> Switch Case Example </h2> <p>Enter a Number Between 1 to 7</p> <input type="text" id="number" /> <button onclick="display_case();">Check</button><br /> <div id="result"><b></b></div> </center> </center> </body> 

你可以这样写:

 switch (varName) { case "afshin": case "saeed": case "larry": alert('Hey'); break; default: alert('Default case'); break; } 

只需切换开关状态即可

 switch (true) { case (function(){ return true; })(): alert('true'); break; case (function(){ return false; })(): alert('false'); break; default: alert('default'); }