Ternary operation in CoffeeScript

前端 未结 7 1598
生来不讨喜
生来不讨喜 2020-12-04 06:56

I need to set value to a that depends on a condition.

What is the shortest way to do this with CoffeeScript?

E.g. this is how I\'d do it in Java

7条回答
  •  攒了一身酷
    2020-12-04 07:16

    You may also write it in two statements if it mostly is true use:

    a = 5
    a = 10 if false
    

    Or use a switch statement if you need more possibilities:

    a = switch x
      when true then 5
      when false then 10
    

    With a boolean it may be oversized but i find it very readable.

提交回复
热议问题