Coffeescript-unless-then-statement-in-coffeescript

提供:Dev Guides
移動先:案内検索

CoffeeScript-except..thenステートメント

*unless-then* ステートメントを使用して、CoffeeScriptの *unless* ステートメントを1行で記述できます。 ブール式の後に *then* キーワードが続き、その後に1つ以上のステートメントが続きます。 これらのステートメントは、指定されたブール式が偽のときに実行されます。

構文

以下は、CoffeeScriptの unless-then ステートメントの構文です。

unless expression then Statement(s) to be executed if expression is false

以下は、CoffeeScriptの unless-then ステートメントの例です。 次の例を unless_then_example.coffee という名前のファイルに保存します

name = "Ramu"
score = 30
unless score>=40 then console.log "Sorry try again"
  • コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c unless_then_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_then_example.coffee

CoffeeScriptファイルを実行すると、次の出力が生成されます。

Sorry try again