Typescript-assignment-operators-examples

提供:Dev Guides
移動先:案内検索

TypeScript-代入演算子の例

Operator Description Example
= (Simple Assignment) Assigns values from the right side operand to the left side operand C = A PLUS B will assign the value of A PLUS B into C
PLUS= (Add and Assignment) It adds the right operand to the left operand and assigns the result to the left operand. C PLUS= A is equivalent to C = C PLUS A
-= (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
AST= (Multiply and Assignment) It multiplies the right operand with the left operand and assigns the result to the left operand. C AST= A is equivalent to C = C AST A
/= (Divide and Assignment) It divides the left operand with the right operand and assigns the result to the left operand.

注意-ビット単位演算子にも同じロジックが適用されるため、⇐=、>> =、>> =、&=、| =、^ =になります。

var a: number = 12
var b:number = 10

a = b
console.log("a = b: "+a)

a += b
console.log("a+=b: "+a)

a -= b
console.log("a-=b: "+a)

a *= b
console.log("a*=b: "+a)

a/= b
console.log("a/=b: "+a)

a %= b
console.log("a%=b: "+a)

コンパイル時に、次のJavaScriptコードが生成されます-

var a = 12;
var b = 10;
a = b;

console.log("a=b: " + a);
a += b;

console.log("a+=b: " + a);
a -= b;

console.log("a-=b: " + a);
a *= b;

console.log("a*=b: " + a);
a/= b;

console.log("a/=b: " + a);
a %= b;
console.log("a%=b: " + a);

それは次の出力を生成します-

a = b: 10
a += b: 20
a -= b: 10
a *= b: 100
a/= b: 10
a %= b: 0