📜  CoffeeScript-运算符和别名

📅  最后修改于: 2020-10-26 05:42:37             🧑  作者: Mango


CoffeeScript运算符

运算符是告诉编译器执行特定数学或逻辑功能的符号。让我们简单地表达4+ 5等于9 。这里的4和5称为操作数,“+”被称为运算符

CoffeeScript提供的运算符与JavaScript中的相同,只是有一些区别。 JavaScript中存在一些有问题的运算符。 CoffeeScript删除了它们或修改了它们的功能,并且还引入了一些新的运算符。

以下是CoffeeScript支持的运算符列表。

  • 算术运算符
  • 比较运算符
  • 逻辑(或关系)运算符
  • 赋值运算符

CoffeeScript别名

除运算符,CoffeeScript还提供别名。 CoffeeScript为各种运算符和符号提供别名,以使您的CoffeeScript代码更具可读性并且更加用户友好。

让我们一一看一下CoffeeScript的所有运算符和别名。

算术运算符

CoffeeScript支持以下算术运算运算符。假设变量A持有10 ,变量B持有20 ,则-

显示范例

S.No Operator and Description Example
1

+ (Addition)

Adds two operands

A + B = 30
2

− (Subtraction)

Subtracts the second operand from the first

A – B = -10
3

* (Multiplication)

Multiply both operands

A * B = 200
4

/ (Division)

Divide the numerator by the denominator

B / A = 2
5

% (Modulus)

Outputs the remainder of an integer division

B % A = 0
6

++ (Increment)

Increases an integer value by one

A++ = 11
7

— (Decrement)

Decreases an integer value by one

A– = 9

比较运算符

JavaScript的支持以下运算符。假设变量A持有10 ,变量B持有20 ,则-

显示范例

S.No Operator and Description Example
1

= = (Equal)

Checks if the value of two operands are equal or not, if yes, then the condition becomes true.

(A == B) is not true.
2

!= (Not Equal)

Checks if the value of two operands are equal or not, if the values are not equal, then the condition becomes true.

(A != B) is true.
3

> (Greater than)

Checks if the value of the left operand is greater than the value of the right operand, if yes, then the condition becomes true.

(A > B) is not true.
4

< (Less than)

Checks if the value of the left operand is less than the value of the right operand, if yes, then the condition becomes true.

(A < B) is true.
5

>= (Greater than or Equal to)

Checks if the value of the left operand is greater than or equal to the value of the right operand, if yes, then the condition becomes true.

(A >= B) is not true.
6

<= (Less than or Equal to)

Checks if the value of the left operand is less than or equal to the value of the right operand, if yes, then the condition becomes true.

(A <= B) is true.

下表显示了一些运算符的别名。假设A持有20 ,变量B持有20

显示范例

Operator Alias Example
= = (Equal) is A is B gives you true.
!= = (Not Equal) isnt A isnt B gives you false.

逻辑运算符

CoffeeScript支持以下逻辑运算符。假设变量A持有10 ,变量B持有20 ,则-

显示范例

S.No Operator and Description Example
1

&& (Logical AND)

If both the operands are non-zero, then the condition becomes true.

(A && B) is true.
2

|| (Logical OR)

If any of the two operands are non-zero, then the condition becomes true.

(A || B) is true.
3

! (Logical NOT)

Reverses the logical state of its operand. If a condition is true, then the Logical NOT operator will make it false.

! (A && B) is false.

下表显示了某些逻辑运算符的别名。假设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) not not X gives you false

按位运算符

CoffeeScript支持以下按位运算运算符。假设变量A持有2 ,变量B持有3 ,则-

显示范例

S.No Operator and Description Example
1

& (Bitwise AND)

It performs a Boolean AND operation on each bit of its integer arguments.

(A & B) is 2.
2

| (BitWise OR)

It performs a Boolean OR operation on each bit of its integer arguments.

(A | B) is 3.
3

^ (Bitwise XOR)

It performs a Boolean exclusive OR operation on each bit of its integer arguments. Exclusive OR means that either operand one is true or operand two is true, but not both.

(A ^ B) is 1.
4

~ (Bitwise Not)

It is a unary operator and operates by reversing all the bits in the operand.

(~B) is -4.
5

<< (Left Shift)

It moves all the bits in its first operand to the left by the number of places specified in the second operand. New bits are filled with zeros. Shifting a value left by one position is equivalent to multiplying it by 2, shifting two positions is equivalent to multiplying by 4, and so on.

(A << 1) is 4.
6

>> (Right Shift)

Binary Right Shift Operator. The left operand’s value is moved right by the number of bits specified by the right operand.

(A >> 1) is 1.

赋值运算符

CoffeeScript支持以下赋值运算符-

显示范例

S.No Operator and Description Example
1

= (Simple Assignment )

Assigns values from the right side operand to the left side operand

C = A + B will assign the value of A + B into C
2

+= (Add and Assignment)

It adds the right operand to the left operand and assigns the result to the left operand.

C += A is equivalent to C = C + A
3

-= (Subtract and Assignment)

It subtracts the right operand from the left operand and assigns the result to the left operand.

C -= A is equivalent to C = C – A
4

*= (Multiply and Assignment)

It multiplies the right operand with the left operand and assigns the result to the left operand.

C *= A is equivalent to C = C * A
5

/= (Divide and Assignment)

It divides the left operand with the right operand and assigns the result to the left operand.

C /= A is equivalent to C = C / A
6

%= (Modules and Assignment)

It takes modulus using two operands and assigns the result to the left operand.

C %= A is equivalent to C = C % A

–相同的逻辑适用于按位运算符,因此它们将变得像<< =,>> =,>> =,&=,| =和^ =。

CoffeeScript中的等式运算符

使用JavaScript时,您会遇到两种等于运算符=====

JavaScript中的==运算符是强制类型,即,如果一个运算中两个操作数的类型不同,则将其中一个运算符的数据类型转换为另一个,然后将两者进行比较。

CoffeeScript的避免了这种不良胁迫,它编译==运算符中的JavaScript严格的比较运算符===。

如果我们使用===比较两个操作数,然后返回true,只有当两者的价值和它们的数据类型相等,否则返回FALSE。

考虑以下示例。这里我们有两个变量aba保留整数类型的值21, b保留相同的值,但它是字符串类型。在CoffeeScript中,当我们比较ab时,结果将为false 。 (由于CoffeeScript的==运算符被转换为JavaScript的===运算符)

a=21
b="21"
result = 21=='21'
console.log result

编译时,上述CoffeeScript会产生以下JavaScript

// Generated by CoffeeScript 1.10.0
(function() {
  var a, b, result;
  
  a = 21;
  
  b = "21";
  
  result = a === b;
  console.log(result);
}).call(this);

执行时,将产生以下输出。

false

存在运算符

CoffeeScript提供了一个新的运算符,称为存在运算符,用于验证变量的存在。用表示。除非变量为null或未定义,否则存在性运算符将返回true。

下面给出了存在运算符的示例。在这里,我们有三个变量,即name,agesubject,并且我们使用存在运算符来验证变量name和phone的存在。

name="Ramu"
age=24
subject="Engineering"
verify_name = name?
verify_phone = phone?
console.log verify_name
console.log verify_phone

编译时,将生成以下JavaScript代码。

// Generated by CoffeeScript 1.10.0
(function() {
  var age, name, subject, verify_name, verify_phone;
  name = "Ramu";
  age = 24;

  subject = "Engineering";
  verify_name = name != null;
  verify_phone = typeof phone !== "undefined" && phone !== null;
  console.log(verify_name);
  console.log(verify_phone);

}).call(this);

如果执行上面的CoffeeScript文件,它将产生以下输出。

true
false

注意-我们有存在运算符的访问器变体吗? 。我们可以使用它代替。运算符以找出空引用。

链式比较

如在Python,我们可以在CoffeeScript的单个表达使用运算符的链。

以下是使用链式比较的示例。

score = 70
passed = 100 > score > 40

console.log passed

编译时,示例CoffeeScript为您提供以下JavaScript代码。

// Generated by CoffeeScript 1.10.0
(function() {
  var passed, score;

  score = 70;

  passed = (100 > score && score > 40);

  console.log(passed);

}).call(this);

如果执行上面的CoffeeScript代码,它将产生以下输出。

true

– CoffeeScript删除三元运算符;代替它,我们可以使用内联if语句。

CoffeeScript别名

通常,CoffeeScript为各种运算符和符号提供别名,以使您的CoffeeScript代码更具可读性并且更加用户友好。以下是CoffeeScript提供的别名。

Name Operator / symbol Aliases
“equals to” operator == is
“not equals to” operator !== isnt
“not” operator ! not
“and” operator && and
“or” operator || or
boolean value true true true, yes, on
boolean value false false off, no
current object this @
new line (or) semi colon \n or ; then
Inverse of if ! if unless
To test for array presence in
To test for object presence of
Exponentiation a**b
Integer division a//b
dividend dependent modulo a%%b

以下示例显示如何在CoffeeScript中使用别名-

a=21; b=21

x = true; y = false

console.log a is b

console.log a isnt b

console.log x and y

console.log x or y

console.log yes or no

console.log on or off

console.log a**b

console.log a//b

console.log a%%b

在编译上面的示例时,它为您提供了以下JavaScript代码。

// Generated by CoffeeScript 1.10.0
(function() {
  var a, b, x, y,
    modulo = function(a, b) { return (+a % (b = +b) + b) % b; };

  a = 21;

  b = 21;

  x = true;

  y = false;

  console.log(a === b);

  console.log(a !== b);

  console.log(x && y);

  console.log(x || y);

  console.log(true || false);

  console.log(true || false);

  console.log(Math.pow(a, b));

  console.log(Math.floor(a / b));

  console.log(modulo(a, b));

}).call(this);

如果执行上述CoffeeScript文件,它将产生以下输出-

true
false
false
true
true
true
5.842587018385982e+27
1
0