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
...
CoffeeScript is a programming language that compiles to JavaScript. It adds syntactic sugar inspired by Ruby, Python, and Haskell in an effort to enhance JavaScript's brevity and readability. Specific additional features include list comprehension and destructuring assignment.
In general, we use parenthesis while declaring the function, calling it, and also to separate the code blocks to avoid ambiguity. In CoffeeScript, there is no need to use parentheses, and while creating functions, we use an arrow mark (->) instead of parentheses as shown below.
The coffee and cake commands will first look in the current folder to see if CoffeeScript is installed locally, and use that version if so. This allows different versions of CoffeeScript to be installed globally and locally.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With