Show 例子 7
优质
小牛编辑
134浏览
2023-12-01
CoffeeScript支持以下赋值运算符 -
Sr.No | 运算符和描述 | 例 |
---|---|---|
1 | = (Simple Assignment ) 将值从右侧操作数分配给左侧操作数 | C = A + B将A + B的值分配给C |
2 | += (Add and Assignment) 它将右操作数添加到左操作数并将结果赋给左操作数。 | C + = A等于C = C + A. |
3 | -= (Subtract and Assignment) 它从左操作数中减去右操作数,并将结果赋给左操作数。 | C - = A相当于C = C - A. |
4 | *= (Multiply and Assignment) 它将右操作数与左操作数相乘,并将结果赋给左操作数。 | C * = A等于C = C * A. |
5 | /= (Divide and Assignment) 它将左操作数与右操作数分开,并将结果赋给左操作数。 | C/= A相当于C = C/A. |
6 | %= (Modules and Assignment) 它使用两个操作数来获取模数,并将结果赋给左操作数。 | C%= A等于C = C%A |
Note - 相同的逻辑适用于按位运算符,因此它们将变为“”=,“”=,“”=,&=,| =和^ =。
例子 (Example)
以下示例演示了CoffeeScript中赋值运算符的用法。 将此代码保存在名为assignment _example.coffee的文件中
a = 33
b = 10
console.log "The value of a after the operation (a = b) is "
result = a = b
console.log result
console.log "The value of a after the operation (a += b) is "
result = a += b
console.log result
console.log "The value of a after the operation (a -= b) is "
result = a -= b
console.log result
console.log "The value of a after the operation (a *= b) is "
result = a *= b
console.log result
console.log "The value of a after the operation (a /= b) is "
result = a /= b
console.log result
console.log "The value of a after the operation (a %= b) is "
result = a %= b
console.log result
打开command prompt并编译.coffee文件,如下所示。
c:/> coffee -c assignment _example.coffee
在编译时,它为您提供以下JavaScript。
// Generated by CoffeeScript 1.10.0
(function() {
var a, b, result;
a = 33;
b = 10;
console.log("The value of a after the operation (a = b) is ");
result = a = b;
console.log(result);
console.log("The value of a after the operation (a += b) is ");
result = a += b;
console.log(result);
console.log("The value of a after the operation (a -= b) is ");
result = a -= b;
console.log(result);
console.log("The value of a after the operation (a *= b) is ");
result = a *= b;
console.log(result);
console.log("The value of a after the operation (a /= b) is ");
result = a /= b;
console.log(result);
console.log("The value of a after the operation (a %= b) is ");
result = a %= b;
console.log(result);
}).call(this);
现在,再次打开command prompt并运行CoffeeScript文件,如下所示。
c:/> coffee assignment _example.coffee
执行时,CoffeeScript文件生成以下输出。
The value of a after the operation (a = b) is
10
The value of a after the operation (a += b) is
20
The value of a after the operation (a -= b) is
10
The value of a after the operation (a *= b) is
100
The value of a after the operation (a /= b) is
10
The value of a after the operation (a %= b) is
0