Show 例子 5

优质
小牛编辑
131浏览
2023-12-01

下表显示了某些逻辑运算符的别名。 假设X保持为true ,变量Y保持为false

操作者别号
&& (Logical AND)andX and Y给你假
|| (逻辑或)orX or Y给你真实
! (不是x)notnot X给你假

例子 (Example)

以下示例演示了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

打开command prompt并编译.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);

现在,再次打开command prompt并运行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