LoginSignup
0
0

More than 5 years have passed since last update.

CoffeeScriptでメソッド定義の条件分岐

Last updated at Posted at 2015-07-28

例えば本番環境とそれ以外の場合で全く違う処理をしたいメソッドを定義する場合どのようにするのが望ましいのだろうか。
もっとも素直な実装はpattern 4であるが、毎回無駄なifが動いてしまうので不快である。

if process.env.NODE_ENV in ['production']
  fn1: -> 
      # Do somthing
      return
else
  fn1: ->
      # Do somthing
      return

class Foo
  # pattern 1
  fn1: fn1

  # pattern 2
  if process.env.NODE_ENV in ['production']
    fn2: -> 
      # Do somthing
      return
  else
    fn2: ->
      # Do somthing
      return

  # pattern 3
  fn3:
    if process.env.NODE_ENV in ['production']
      ->
        # Do somthing
        return
    else
      ->
        # Do somthing
        return

  # pattern 4
  fn4: ->
    if process.env.NODE_ENV in ['production']
      # Do somthing
      return
    else
      # Do somthing
      return

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0