Coffeescript-logical-aliases
提供:Dev Guides
CoffeeScript-論理演算子のエイリアス
次の表に、いくつかの論理演算子のエイリアスを示します。 X が true を保持し、変数 Y が false を保持するとします。
Operator | Alias | Example |
---|---|---|
&& (Logical AND) | and | X and Y gives you false |
(Logical OR) | ||
or | X or Y gives you true | ! (not x) |
例
次の例は、CoffeeScriptの論理演算子のエイリアスの使用を示しています。 このコードを logical_aliases.coffee という名前のファイルに保存します。
a = true
b = false
console.log "The result of (a and b) is "
result = a and b
console.log result
console.log "The result of (a or b) is "
result = a or b
console.log result
console.log "The result of not(a and b) is "
result = not(a and b)
console.log result
- コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c logical_aliases.coffee
コンパイル時に、次のJavaScriptが提供されます。
//Generated by CoffeeScript 1.10.0
(function() {
var a, b, result;
a = true;
b = false;
console.log("The result of (a and b) is ");
result = a && b;
console.log(result);
console.log("The result of (a or b) is ");
result = a || b;
console.log(result);
console.log("The result of not(a and b) is ");
result = !(a && b);
console.log(result);
}).call(this);
次に、*コマンドプロンプト*を再度開き、以下に示すようにCoffeeScriptファイルを実行します。
c:\> coffee logical_aliases.coffee
CoffeeScriptファイルを実行すると、次の出力が生成されます。
The result of (a and b) is
false
The result of (a or b) is
true
The result of not(a and b) is
true