【JavaScript】【jQuery】条件でメソッドを切り替えるときの書き方
if(flag){ $(".the .longest .selector .in .the .world").show().anotherMethod(); }else{ $(".the .longest .selector .in .the .world").hide().anotherMethod(); }
こんなコードを書いて、「なんか違うわ」と感じたときありませんか。
そういうときは
$(".the .longest .selector .in .the .world")[flag?"show":"hide"]().anotherMethod();
可読性は若干落ちるのかな?
けど5行が1行になるしね。
よく使う人には常識かもしれない話で失礼しやした。