Coffeescript-unless-then-else-statement-in-coffeescript
提供:Dev Guides
except-then … elseステートメント
except-thenステートメントの後にオプションの else ステートメントを続けることができます。これはブール式がtrueの場合に実行されます。 except-then … elseステートメントを使用すると、unless … elseステートメントを1行で記述できます。
構文
以下は、CoffeeScriptの unless-then else ステートメントの構文です。
unless expression then Statements (for false) else Statements (for true)
例
以下に、CoffeeScriptの unless-then else ステートメントの例を示します。 次の例を unless_then_else_example.coffee という名前のファイルに保存します
name = "Ramu"
score = 60
unless score>=40 then console.log "Sorry try again" else console.log "congratulations."
- コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c unless_then_else_example.coffee
コンパイル時に、次のJavaScriptが提供されます。
//Generated by CoffeeScript 1.10.0
(function() {
var name, score;
name = "Ramu";
score = 60;
if (!(score >= 40)) {
console.log("Sorry try again");
} else {
console.log("congratulations.");
}
}).call(this);
ここで、*コマンドプロンプト*を再度開き、CoffeeScriptファイルを次のように実行します-
c:\> coffee unless_then_else_example.coffee
CoffeeScriptファイルを実行すると、次の出力が生成されます。
congratulations.