There are two forms of switch
in CoffeeScript:
switch expr
when expr1
result1
...
else
default_result
and
switch
when expr1
result1
...
else
default_result
Both forms are, of course, expressions so you can say:
x = switch expr
when val then result
...
and
x = switch
when expr then result
...
The switch expr
is like a JavaScript switch
: you compare the result of expr
with each when
expression and execute the when
that is ==
to expr
. This form of switch
is the same as:
if(expr == expr1)
result1
else if(expr == expr2)
result2
...
else
default_result
The switch ... when
form executes the first when expr
where expr
is true so it is a fancy way of saying:
if(expr1)
result1
else if(expr2)
result2
...
else
default_result
All of your attempts appear to be trying to mix the two forms of switch
and confusion ensues.
I think you're looking for something like this:
arr = "coffee_script"
switch arr
when 'coffee_script'
item.type = arr
...