2014/03/14

[coffeescript]coffeescriptでのswitch文について

coffeescriptではswitch文が用意されているのですが、caseではなくwhenで記述するようだ。

実際に組んでみる。

hoge = "foo"
switch hoge
  when "foo"
    console.log "this is foo"
  when "bar"
    console.log "this is bar"
  else
    console.log "this is not bar and foo"
これをコンパイルした結果は下のようになります。
// Generated by CoffeeScript 1.5.0
var hoge;

hoge = "foo";

switch (hoge) {
  case "foo":
    console.log("this is foo");
    break;
  case "bar":
    console.log("this is bar");
    break;
  default:
    console.log("this is not bar and foo");
}
ちゃんと出力されていますね。

0 コメント:

コメントを投稿