2011-03-10 68 views
1

如果是,为什么这段代码不会提醒任何事情?做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> 
+0

无论如何,这个开关有什么意义?为什么要将常量与其他常量进行比较? – 2011-03-10 14:01:12

+0

它适用于Chrome。 – 2011-03-10 14:02:55

+1

按我的预期工作,ff3.6 – meouw 2011-03-10 14:03:15

回答

1

Works for me在...

  • 火狐3.6
  • 铬10

一个开关,你一般把它比作常量。你使用它的方式没有什么意义。

0

是的,它接受expressionsFrom the specification

CaseClause
情况下表达语句列表选择

而且随着其他人说,它似乎为他们工作。你的真实代码中可能有另一个错误。