LoginSignup
0
0

More than 5 years have passed since last update.

JS の最少記述の検討 with Closure Compiler

Posted at

AND OR 条件をうまくやればもっと綺麗に書けるんだろうなぁと思いつつ頭が足りないので Closure Compiler Service の力を借りる。

JS の処理を取り敢えず minify して条件式に戻す策にて。

三項演算子の訳分からんくなった処理を、取り敢えず処理部分を数字に置き換えてみた。

元々のがこうだとすると

if (1) {
    if (!2 && !3) 4;
    else 5;
} else if (6) 7;
else 5;

こんな感じになる。

1 ? 2 || 3 ? 5 : 4 : 6 ? 7 : 5;

それでも良く分からんので、

適当に Chrome の console にぶち込んで数字の前に ! を付けて分岐を調べつインデントを付けてたら、分岐が大よそ分かった。

1 ?
    2 || 3 ?
        5 :
        4
    :
    6 ?
        7:
        5;

これをまた if 分に戻すと ! が削れて若干シンプルになったので、喜んでコレで終わりに。

if (1) {
    if (2 || 3) 5;
    else 4;
} else if (6) 7;
else 5;
0
0
1

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