Javascript 用于复杂情况的switch语句的较短版本
是否可以将此声明缩短?我知道,如果情况类似于1、2、3等,则switch语句有速记Javascript 用于复杂情况的switch语句的较短版本,javascript,Javascript,是否可以将此声明缩短?我知道,如果情况类似于1、2、3等,则switch语句有速记 var direction = pos[i].direction; switch (true) { case (direction >= 0 && direction < 22): graphic = "/img/0.png" break; case (direction >= 22
var direction = pos[i].direction;
switch (true) {
case (direction >= 0 && direction < 22):
graphic = "/img/0.png"
break;
case (direction >= 22 && direction < 45):
graphic = "/img/225.png"
break;
case (direction >= 45 && direction < 67):
graphic = "/img/450.png"
break;
case (direction >= 67 && direction < 90):
graphic = "/img/675.png"
break;
default:
graphic = "/img/0.png"
break;
}
var-direction=pos[i]。方向;
开关(真){
情况(方向>=0和方向<22):
graphic=“/img/0.png”
打破
情况(方向>=22和方向<45):
graphic=“/img/225.png”
打破
外壳(方向>=45和方向<67):
graphic=“/img/450.png”
打破
外壳(方向>=67和方向<90):
graphic=“/img/675.png”
打破
违约:
graphic=“/img/0.png”
打破
}
您可以省略第一次检查,因为之前已经检查了该值
switch (true) {
case direction < 22:
graphic = "/img/0.png";
break;
case direction < 45:
graphic = "/img/225.png";
break;
case direction < 67:
graphic = "/img/450.png";
break;
case direction < 90:
graphic = "/img/675.png";
break;
default:
graphic = "/img/0.png";
break;
}
开关(真){
病例方向<22:
graphic=“/img/0.png”;
打破
病例方向<45:
graphic=“/img/225.png”;
打破
病例方向<67:
graphic=“/img/450.png”;
打破
病例方向<90:
graphic=“/img/675.png”;
打破
违约:
graphic=“/img/0.png”;
打破
}
您可以省略第一次检查,因为之前已经检查了该值
switch (true) {
case direction < 22:
graphic = "/img/0.png";
break;
case direction < 45:
graphic = "/img/225.png";
break;
case direction < 67:
graphic = "/img/450.png";
break;
case direction < 90:
graphic = "/img/675.png";
break;
default:
graphic = "/img/0.png";
break;
}
开关(真){
病例方向<22:
graphic=“/img/0.png”;
打破
病例方向<45:
graphic=“/img/225.png”;
打破
病例方向<67:
graphic=“/img/450.png”;
打破
病例方向<90:
graphic=“/img/675.png”;
打破
违约:
graphic=“/img/0.png”;
打破
}
您应该使用if-else,因为您已经准备好了条件。所以不需要使用switch-case。您应该使用if-else,因为您已经准备好了条件。所以不需要使用switch-case。您好,您也可以省略第一种情况,因为它是默认情况。否,然后,您需要检查大于或等于22
以及小于45
的值。看起来,graphic=“/img/0.png”
是小于22
或大于/等于90
的默认值。您好,您也可以忽略第一种情况,因为它是默认值。否,然后需要检查大于或等于22
以及小于45
的值。看起来,graphic=“/img/0.png”
是小于22
或大于/等于90
的默认值。