Coffeescript-postfix-if-and-unless
提供:Dev Guides
CoffeeScript-後置のifおよびexceptステートメント
接尾辞if
後置形式を使用してif文を書き換えることができます。whereの場合、実行されるステートメントの後にifとブール式が続きます。
構文
以下は、postfix-ifステートメントの構文です。
Statements to be executed if expression
例
以下は、接尾辞ifステートメントの例です。 次の例を postfix_if_example.coffee という名前のファイルに保存します
name = "Ramu"
score = 60
console.log "Congratulations you have passed the examination" if score>40
- コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c postfix_if_example.coffee
コンパイル時に、次のJavaScriptが提供されます。
//Generated by CoffeeScript 1.10.0
(function() {
var name, score;
name = "Ramu";
score = 60;
if (score > 40) {
console.log("Congratulations you have passed the examination");
}
}).call(this);
ここで、*コマンドプロンプト*を再度開き、CoffeeScriptファイルを次のように実行します-
c:\> coffee postfix_if_example.coffee
CoffeeScriptファイルを実行すると、次の出力が生成されます。
Congratulations you have passed the exam
後置
後置形式を使用して、unless-statementを書き換えることができます。ここで、実行されるステートメントの後には、boolean式と共にexceptが続きます。
構文
以下は、postfix-ifステートメントの構文です。
Statements to be executed unless expression
例
以下に、postfix exceptステートメントの例を示します。 次の例を postfix_unless_example.coffee という名前のファイルに保存します
name = "Ramu"
score = 30
console.log "Sorry try again" unless score>=40
- コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c postfix_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 postfix_unless_example.coffee
CoffeeScriptファイルを実行すると、次の出力が生成されます。
Sorry try again