目录

Show 例子

CoffeeScript支持以下逻辑运算符。 假设变量A保持为true ,变量B保持为false ,则 -

Sr.No 运算符和描述
1

&& (Logical AND)

如果两个操作数均为真,则条件成立。

(A && B)是假的。
2

|| (Logical OR)

如果两个操作数中的任何一个为真,则条件成立。

(A || B)是真的。
3

! (Logical NOT)

反转其操作数的逻辑状态。 如果条件为真,则Logical NOT运算符将使其为false。

! (A && B)是真的。

例子 (Example)

以下是演示在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

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

现在,再次打开command prompt并运行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
↑回到顶部↑
WIKI教程 @2018