CoffeeScript - operatory przypisania
CoffeeScript obsługuje następujące operatory przypisania -
| Sr.No | Operator i opis | Przykład |
|---|---|---|
| 1 | = (Simple Assignment ) Przypisuje wartości z prawego operandu do lewego operandu |
C = A + B przypisze wartość A + B do C. |
| 2 | += (Add and Assignment) Dodaje prawy operand do lewego operandu i przypisuje wynik do lewego operandu. |
C + = A jest równoważne C = C + A |
| 3 | -= (Subtract and Assignment) Odejmuje prawy operand od lewego operandu i przypisuje wynik do lewego operandu. |
C - = A jest równoważne C = C - A |
| 4 | *= (Multiply and Assignment) Mnoży prawy operand przez lewy operand i przypisuje wynik lewemu operandowi. |
C * = A jest równoważne C = C * A |
| 5 | /= (Divide and Assignment) Dzieli lewy operand z prawym operandem i przypisuje wynik lewemu operandowi. |
C / = A jest równoważne C = C / A |
| 6 | %= (Modules and Assignment) Pobiera moduł używając dwóch operandów i przypisuje wynik do lewego operandu. |
C% = A jest równoważne C = C% A |
Note - Ta sama logika dotyczy operatorów bitowych, więc będą wyglądać jak << =, >> =, >> =, & =, | = i ^ =.
Przykład
Poniższy przykład ilustruje użycie operatorów przypisania w CoffeeScript. Zapisz ten kod w pliku o nazwieassignment _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
Otworzyć command prompt i skompiluj plik .coffee, jak pokazano poniżej.
c:/> coffee -c assignment _example.coffee
Podczas kompilacji daje następujący 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);
Teraz otwórz command prompt ponownie i uruchom plik CoffeeScript, jak pokazano poniżej.
c:/> coffee assignment _example.coffee
Podczas wykonywania plik CoffeeScript generuje następujące dane wyjściowe.
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