0

これを解決するエレガントな方法はありますか?

if (condition0) {
  if(condition1) {
    do thing 1
  }
  else if(condition2){
    do thing 2
  }
}
else {
  if(condition2) {
    do thing 2
  }
  else if(condition1){
    do thing 1
  }
}

do thing 1多くのパラメーターを使用した関数呼び出しとdo thing 2、どういうわけか不必要な繰り返しがあるようです。

これを行うより良い方法はありますか?

4

2 に答える 2

2
if (condition1 && (condition0 || !condition2)) {
  do thing 1
} else if (condition2) {
  do thing 2
}
于 2013-04-27T12:02:13.287 に答える
1

コードの繰り返しを避けるために、do Thing 1 と Do Thing 2 を関数に格納できます。きれいにするために。

var DoThing1 = function ()
{
   do thing 1
}

var DoThing2 = function ()
{
    do thing 2
}
if (condition0) {
    if(condition1) {
        DoThing1();
    }
    else if(condition2){
        DoThing2();
    }
}
else {
    if(condition2) {
        DoThing2(); 
    }
    else if(condition1){
        DoThing1();
    }
}
于 2013-04-27T11:58:54.360 に答える