Coffeescript-assignment-operators
提供:Dev Guides
CoffeeScript-割り当て演算子
CoffeeScriptは次の割り当て演算子をサポートしています-
Sr.No | Operator and Description | Example |
---|---|---|
1 |
= (Simple Assignment ) 右側のオペランドから左側のオペランドに値を割り当てます |
C = A PLUS B will assign the value of A PLUS B into C |
2 |
PLUS= (Add and Assignment) 右オペランドを左オペランドに追加し、結果を左オペランドに割り当てます。 |
C PLUS= A is equivalent to C = C PLUS A |
3 |
-= (Subtract and Assignment) 左のオペランドから右のオペランドを減算し、結果を左のオペランドに割り当てます。 |
C -= A is equivalent to C = C - A |
4 |
*= (Multiply and Assignment) 右オペランドと左オペランドを乗算し、結果を左オペランドに割り当てます。 |
C *= A is equivalent to C = C *A |
5 |
/= (Divide and Assignment) 左のオペランドを右のオペランドで除算し、結果を左のオペランドに割り当てます。 |
C/= A is equivalent to C = C/A |
6 |
%= (Modules and Assignment) 2つのオペランドを使用してモジュラスを取得し、結果を左のオペランドに割り当てます。 |
C %= A is equivalent to C = C % A |
注-ビット単位演算子にも同じロジックが適用されるため、⇐=、>> =、>> =、&=、| =、^ =のようになります。
例
次の例は、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
- コマンドプロンプト*を開き、以下に示すように.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);
次に、*コマンドプロンプト*を再度開き、以下に示すように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