Sunday, May 12, 2024
173
rated 0 times [  179] [ 6]  / answers: 1 / hits: 24097  / 12 Years ago, tue, may 8, 2012, 12:00:00

Is it possible to use switch in coffeescript without break?



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
...

More From » coffeescript

 Answers
17

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.




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



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

[#85713] Sunday, May 6, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
anjelicadixied

Total Points: 742
Total Questions: 94
Total Answers: 97

Location: Iraq
Member since Fri, Jun 5, 2020
4 Years ago
;