do javascript switch 语句 case 允许使用表达式
如果是,为什么此代码不发出任何警报?
<html>
<head>
<script type="text/javascript">
switch ("hello world") {
case "hello" + " world":
alert("Greeting was found.");
break;
case "goodbye":
alert("Closing was found.");
break;
default:
alert("Unexpected message was found.");
}
</head>
</html>
if yes, why this code doesn't alert anything?
<html>
<head>
<script type="text/javascript">
switch ("hello world") {
case "hello" + " world":
alert("Greeting was found.");
break;
case "goodbye":
alert("Closing was found.");
break;
default:
alert("Unexpected message was found.");
}
</head>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对我有用...
使用开关时,您通常将其与常量进行比较。您使用它的方式没有多大意义。
Works for me in...
With a switch, you generally compare it to constants. The way you are using it doesn't make much sense.
是的,它接受表达式。 来自规范:
正如其他人所说,它似乎对他们有用。可能您的实际代码中还有另一个错误。
Yes, it accepts expressions. From the specification:
And as the others say, it seems to work for them. Probably you have another error in your real code.