14

我想使用一个简单的switch语句,但它不能编译。代码如下:Coffeescript:意外,然后在switch语句

tag = 0 
switch tag 
    when 0 then 
     alert "0" 
    when 1 then 
     alert "1" 

coffeescript编译器抱怨switch语句后的行中出现“unexpected then”。 我改变了代码如下:

switch tag 
    when 0 then alert "0" 
    when 1 then alert "1" 

,它工作正常。

但我需要在switch语句的then部分中的多行上有多条语句。这是不可能的吗?

回答

30

完全删除then。只有当你不想有新的缩进块时才需要它。

tag = 0 
switch tag 
    when 0 
     alert "0" 
    when 1 
     alert "1" 

if这样工作,太)