4
如何使用coffescript中的賦值開關? 嘗試了很多方法,但無法使其工作。coffescript中的開關變量賦值
item = { name: "ahola" }
arr = "coffee_script"
switch arr
when arr.match /script/
item.type = arr
alert item.name + " : " + item.type
# alerts "ahola : undefined"
即使這不工作
item = { name: "ahola" }
arr = "coffee_script"
switch arr
when arr == "coffee_script"
item.type = arr
alert item.name + " : " + item.type
# alerts "ahola : undefined"
試過這種方式也
item = { name: "ahola" }
arr = "coffee_script"
item.type = switch arr
when arr.match /script/
arr
alert item.name + " : " + item.type
# alerts "ahola : undefined"
最後發現這也不起作用
item = { name: "ahola" }
arr = "coffee_script"
item.type = switch arr
when true
arr
alert item.name + " : " + item.type
# alerts "ahola : undefined"
我錯過什麼?
好了,這是一個很好的解釋。謝謝。 – roxxypoxxy