Coffeescript-unless-statement-in-coffeescript
提供:Dev Guides
CoffeeScript-exceptステートメント
*unless* ステートメントは、CoffeeScriptが提供する追加機能です。 *if* ステートメントの正反対です。 指定された条件が *false* の場合、 *unless* ブロック内のコードが実行されます。
構文
以下に、CoffeeScriptの unless ステートメントの構文を示します。 if ステートメントと同様に、 unless キーワード、ブール式、および指定された式が false の場合に実行されるコードブロックが含まれます。
unless expression
Statement(s) to be executed if the expression is false
流れ図
例
次の例は、CoffeeScriptでの unless ステートメントの使用法を示しています。 このコードを unless_example.coffee という名前のファイルに保存します
name = "Ramu"
score = 30
unless score>=40
console.log "Sorry try again"
- コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c unless_example.coffee
コンパイル時に、次のJavaScriptが提供されます。
//Generated by CoffeeScript 1.10.0
(function() {
var name, score;
name = "Ramu";
score = 30;
if (!(score >= 40)) {
console.log("Sorry try again");
}
}).call(this);
ここで、*コマンドプロンプト*を再度開き、CoffeeScriptファイルを次のように実行します-
c:\> coffee unless_example.coffee
CoffeeScriptファイルを実行すると、次の出力が生成されます。
Sorry try again