coffeescript重构

dag*_*da1 3 coffeescript

是否有任何coffeescript特定的技巧,使这看起来更整洁:

index = (->
          if segment == 'index'
            return 0
          else if segment == 'inbox'
            return 2

          1
        )()
Run Code Online (Sandbox Code Playgroud)

epi*_*ian 7

是的,一个switch表达式:

index = switch segment
  when 'index' then 0
  when 'inbox' then 2
  else 1
Run Code Online (Sandbox Code Playgroud)