CoffeeScript - 逻辑运算符
CoffeeScript 支持以下逻辑运算符。 假设变量A 为true,变量B 为false,则 −
| Sr.No | 运算符和说明 | 示例 |
|---|---|---|
| 1 | && (逻辑与) 如果两个操作数都为真,则条件为真。 |
(A && B) is false. |
| 2 | || (逻辑或) 如果两个操作数中的任何一个为真,则条件变为真。 |
(A || B) is true. |
| 3 | ! (逻辑非) 反转其操作数的逻辑状态。 如果条件为真,则逻辑非运算符将使它为假。 |
! (A && B) is true. |
示例
以下是演示在 coffeeScript 中使用逻辑运算符的示例。 将此代码保存在名为 logical_example.coffee 的文件中。
a = true b = false console.log "The result of (a && b) is " result = a && b console.log result console.log "The result of (a || b) is " result = a || b console.log result console.log "The result of !(a && b) is " result = !(a && b) console.log result
打开命令提示符并编译.coffee 文件,如下所示。
c:\> coffee -c logical_example.coffee
在编译时,它会提供以下 JavaScript。
// Generated by CoffeeScript 1.10.0
(function() {
var a, b, result;
a = true;
b = false;
console.log("The result of (a && b) is ");
result = a && b;
console.log(result);
console.log("The result of (a || b) is ");
result = a || b;
console.log(result);
console.log("The result of !(a && b) is ");
result = !(a && b);
console.log(result);
}).call(this);
现在,再次打开命令提示符 并运行 CoffeeScript 文件,如下所示。
c:\> coffee logical_example.coffee
执行时,CoffeeScript 文件产生以下输出。
The result of (a && b) is false The result of (a || b) is true The result of !(a && b) is true

