2013/04/20

[JavaScript][CoffeeScript]後置のifについて

条件分岐(if, else など)」を読むと、後置のifが使えるとあったがちょっとわからなかったので、実際にプログラムを組んでみることに。

bar = "test1"
hoge = false
bar = "test2" if hoge
console.log(bar)
hoge = true
bar = "test3" if hoge
console.log(bar)
#出力結果
#test1
#test3
で上のケースでは、if文の後が、一語だったので、文章でも成り立つか確認してみた。
bar = "test1"
hoge = "hello"
bar = "test2" if hoge == "hello"
console.log(bar)
bar = "test3" if hoge == "hello1"
console.log(bar)
#出力結果
#test2
#test2
どうやら文章でも成立するみたいです。

0 コメント:

コメントを投稿