CoffeeScript - 逻辑运算符的别名
下表显示了一些逻辑运算符的别名。 假设变量 X 持有 true 和变量 Y 持有 false。
| 运算符 | 别名 | 示例 |
|---|---|---|
| && (逻辑与) | and | X and Y gives you false |
| || (逻辑或) | or | X or Y gives you true |
| ! (逻辑非) | not | not X gives you false |
示例
以下示例演示了在 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

