Javascript 咖啡脚本无间断切换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10502079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:36:54  来源:igfitidea点击:

coffee script switch without break

javascriptcoffeescript

提问by puchu

Is it possible to use switch in coffeescript without break?

是否可以在咖啡脚本中不间断地使用 switch?

switch code                      switch (code) {
    when 37 then                     case 37: break;
    when 38 then           ->        case 38: break;
    when 39 then                     case 39: break;
    when 40                          case 40:
        ...                              ...

I thought this will work but failed:

我认为这会奏效,但失败了:

switch code
    when 37 then continue
    when 38 then continue  ->    not valid
    when 39 then continue
    when 40
        ...

回答by Linus Gustav Larsson Thiel

Not really. From the docs:

并不真地。从文档

Switch statements in JavaScript are a bit awkward. You need to remember to break at the end of every case statement to avoid accidentally falling through to the default case. CoffeeScript prevents accidental fall-through, and can convert the switch into a returnable, assignable expression. The format is: switch condition, when clauses, else the default case.

JavaScript 中的 switch 语句有点笨拙。您需要记住在每个 case 语句的末尾打断,以避免意外落入默认 case。CoffeeScript 可以防止意外失败,并且可以将 switch 转换为可返回、可赋值的表达式。格式为:switch 条件,when 子句,else 默认情况。

What you can do, though, is specify several values in a case, if they are to be treated equally:

但是,case如果要平等对待它们,您可以在 a 中指定多个值:

switch day
  when "Mon" then go work
  when "Tue" then go relax
  when "Thu" then go iceFishing
  when "Fri", "Sat"
    if day is bingoDay
      go bingo
      go dancing
  when "Sun" then go church
  else go work

回答by Ron Martinez

You can use line continuation to help with this. For example:

您可以使用换行符来帮助解决此问题。例如:

name = 'Jill'

switch name
  when 'Jill', \
       'Joan', \
       'Jess', \
       'Jean'
    $('#display').text 'Hi!'
  else
    $('#display').text 'Bye!'

Check it out in action here.

在这里查看它的实际效果。

回答by portforwardpodcast

It's totally possible, just use a classic javascript and pass it through with backtics

这是完全可能的,只需使用经典的 javascript 并通过 backtics 传递它

`
switch (code) {
    case 37:
    case 38:
    case 39:
    case 40:
        // do the work of all four
    default:
        //default
}
`

回答by Koen.

Old question already, but if you place the commas on the next line, it works as expected, without the backslash line continuation showed by @Ron Martinez

老问题了,但如果你把逗号放在下一行,它会按预期工作,没有@Ron Martinez 显示的反斜杠行延续

switch code
  when 37
     , 38
     , 39
     , 40
    console.log "Some Number"
  else
    console.log "Default"

Which will compile to:

这将编译为:

switch (code) {
  case 37:
  case 38:
  case 39:
  case 40:
    return console.log("Some Number");
  default:
    return console.log("Default");
}