Coffeescript 简明教程
CoffeeScript - Operators and Aliases
CoffeeScript Operators
运算符是一个符号,告诉编译器执行特定的数学或逻辑函数。让我们来看一个简单的表达式 4 + 5 is equal to 9 。此处 4 和 5 称为 operands ,而 “+” 称为 operator 。
An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. Let us take a simple expression 4 + 5 is equal to 9. Here 4 and 5 are called operands and ‘+’ is called the operator.
CoffeeScript 提供的运算符与 JavaScript 中的运算符相同,但有一些区别。在 JavaScript 中有一些有问题的运算符。CoffeeScript 或者移除了它们,或者修改了它们的功能,并且还引入了一些新的运算符。
The operators provided by CoffeeScript are same as in JavaScript except a few differences. There are some problematic operators in JavaScript. CoffeeScript either removed them or modified their functionality and it also introduced some new operators.
以下是 CoffeeScript 支持的运算符列表。
Following is the list of operators supported by CoffeeScript.
-
Arithmetic Operators
-
Comparison Operators
-
Logical (or Relational) Operators
-
Assignment Operators
CoffeeScript Aliases
除了运算符外,CoffeeScript 还提供别名。CoffeeScript 为各种运算符和符号提供别名,以便使你的 CoffeeScript 代码具有可读性并且更加用户友好。
In addition to operators, CoffeeScript also provides aliases. CoffeeScript provides aliases to various operators and symbols in order to make your CoffeeScript code readable and more user friendly.
让我们逐个了解 CoffeeScript 的所有运算符和别名。
Let us have a look at all the operators and aliases of CoffeeScript one by one.
Arithmetic Operators
CoffeeScript 支持以下算术运算符。假设变量 A 保存 10 并且变量 B 保存 20 ,则 −
CoffeeScript supports the following arithmetic operators. Assume variable A holds 10 and variable B holds 20, then −
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 |
Comparison Operators
JavaScript 支持以下比较运算符。假设变量 A 保存 10 并且变量 B 保存 20 ,则 −
JavaScript supports the following comparison operators. Assume variable A holds 10 and variable B holds 20, then −
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 。
Following table shows the aliases for few of the Comparison operators. Suppose A holds 20 and variable B holds 20.
Operator |
Alias |
Example |
= = (Equal) |
is |
A is B gives you true. |
!= = (Not Equal) |
isnt |
A isnt B gives you false. |
Logical Operators
CoffeeScript 支持以下逻辑运算符。假设变量 A 作用 10 ,变量 B 作用 20 ,那么 −
CoffeeScript supports the following logical operators. Assume variable A holds 10 and variable B holds 20, then −
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. |
下表显示了少数逻辑运算符的别名。假设 X 作用 true ,变量 Y 作用 false 。
The following table shows the aliases for some of the logical operators. Suppose X holds true and variable Y holds false.
Operator |
Alias |
Example |
&& (Logical AND) |
and |
X and Y gives you false |
(Logical OR) |
||
or |
X or Y gives you true |
! (not x) |
Bitwise Operators
CoffeeScript 支持以下逐位运算符。假设变量 A 作用 2 ,变量 B 作用 3 ,那么 −
CoffeeScript supports the following bitwise operators. Assume variable A holds 2 and variable B holds 3, then −
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 |
Assignment Operators
Coffeescript 支持以下赋值运算符 -
CoffeeScript supports the following assignment operators −
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 |
Note − 同样的逻辑适用于按位运算符,所以它们将变成 <⇐, >>=, >>=, &=, |= 和 ^=.
Note − Same logic applies to Bitwise operators so they will become like <⇐, >>=, >>=, &=, |= and ^=.
Equality Operator in CoffeeScript
在使用 JavaScript 时,将遇到两种类型的相等运算符 == 和 === 。
While working with JavaScript, you will encounter two types of equality operators == and ===.
JavaScript 中的 == 运算符为 type coercive ,即如果运算中两个操作数的类型不同,则一个操作数的数据类型将被转换为另一个并进行比较。
The == operator in JavaScript is type coercive, i.e., if the types of the two operands in an operation are different, then the data type of one of the operator is converted into other and then both are compared.
CoffeeScript 避免了这种不受欢迎的强制,将 == 运算符编译到 JavaScript 的严格比较操作符 === 中。
CoffeeScript avoids this undesirable coercion, it compiles the == operator in to the strict comparison operator of JavaScript ===.
如果我们用 === 比较两个操作符,只有当它们的值和数据类型相同时,它才返回 true ,否则它返回 false 。
If we compare two operands using ===, then it returns true, only if both the value and datatypes of them are equal, else it returns false.
Example
考虑以下示例。这里我们有两个变量 a 和 b 。 a 持有整数类型的值 21, b 持有相同的值,但它属于 string 类型。在 CoffeeScript 中,当我们比较 a 和 b 时,结果将为 false 。(由于 CoffeeScript 的 == 操作符被转换为 JavaScript 的 === 操作符)
Consider the following example. Here we have two variables a and b. a holds the value 21 of integer type and b holds the same value, but it is of string type. In CoffeeScript, when we compare a and b, the result will be false. (Since the == operator of CoffeeScript is converted to === operator of JavaScript)
a=21
b="21"
result = 21=='21'
console.log result
编译时,上述 CoffeeScript 产生以下 JavaScript
On compiling, the above CoffeeScript produces the following JavaScript
// Generated by CoffeeScript 1.10.0
(function() {
var a, b, result;
a = 21;
b = "21";
result = a === b;
console.log(result);
}).call(this);
执行时,它会产生以下输出。
On executing, it produces the following output.
false
The existential Operator
CoffeeScript 提供了一个称为存在操作符的新操作符来验证变量的存在。它由 ? 表示。除非变量为 null 或 undefined,否则存在操作符返回真。
CoffeeScript provides a new operator known as existential operator to verify the existence of a variable. It is denoted by ?. Unless a variable is null or undefined, the existential operator returns true.
Example
下面给出了存在操作符的一个示例。这里我们有三个变量,即 name, age 和 subject ,我们正在使用存在操作符来验证变量 name 和 phone 的存在。
Given below is an example of the existential operator. Here we have three variables, namely name, age, and subject and we are verifying the existence of the variables name and phone using existential operator.
name="Ramu"
age=24
subject="Engineering"
verify_name = name?
verify_phone = phone?
console.log verify_name
console.log verify_phone
编译时,这将生成以下 JavaScript 代码。
On compiling, this will generate the following JavaScript code.
// 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 文件,它将产生以下输出。
If we execute the above CoffeeScript file, it produces the following output.
true
false
Note − 我们有存在操作符的访问器变体 ? 。我们可以使用它来代替 . 操作符来找出空引用。
Note − We have an accessor variant of the existential operator ?. We can use it instead of the . operator to find out the null references.
Chained Comparisons
像在 Python 中一样,我们可以在 CoffeeScript 中的单个表达式中使用一连串比较操作符。
As in Python, we can use a chain of comparison operators in a single expression in CoffeeScript.
Example
以下是使用链式比较的一个示例。
Following is an example of using chained comparison.
score = 70
passed = 100 > score > 40
console.log passed
编译时,示例 CoffeeScript 为您提供以下 JavaScript 代码。
On compiling, the example CoffeeScript gives you the following JavaScript code.
// Generated by CoffeeScript 1.10.0
(function() {
var passed, score;
score = 70;
passed = (100 > score && score > 40);
console.log(passed);
}).call(this);
如果您执行上述 CoffeeScript 代码,它将产生以下输出。
If you execute the above CoffeeScript code, it produces the following output.
true
Note − CoffeeScript 移除了三元操作符;相反,我们可以使用 inline if 语句。
Note − CoffeeScript removes the ternary operator; instead of it, we can use the inline if statement.
CoffeeScript Aliases
通常,CoffeeScript 为各种操作符和符号提供别名,以使您的 CoffeeScript 代码可读且更易于用户使用。以下是 CoffeeScript 提供的别名。
In general, CoffeeScript provides aliases to various operators and symbols in order to make your CoffeeScript code readable and more user friendly. Following are the aliases provided by 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 |
Example
以下示例显示了如何在 CoffeeScript 中使用别名 -
The following example shows how to use aliases in 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 代码。
On compiling the above example, it gives you the following JavaScript code.
// 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 文件,它会生成以下输出 −
If you execute the above CoffeeScript file, it produces the following output −
true
false
false
true
true
true
5.842587018385982e+27
1
0