2013/04/21

[JavaScript][CoffeeScript]1行のif-elseについて

昨日に続いて「条件分岐(if, else など)」を読むと、1行でif-else文が書けるとのことでさっそく試してみた。

hoge = true
bar = if hoge then "test1" else "test2"
console.log(bar)

hoge = false
bar = if hoge then "test1" else "test2"
console.log(bar)

#出力結果
#test1
#test2
上のif文は、変数が1つだけの評価だったが、文章で評価できるのか試してみた。
hoge = "hello"
bar = if hoge == "hello" then "test1" else "test2"
console.log(bar)

hoge = "hello"
bar = if hoge == "hello1" then "test1" else "test2"
console.log(bar)

#出力結果
#test1
#test2
どうやらできるみたい。

0 コメント:

コメントを投稿