Assignment and Compound Assignment

     

The assignment operator ( = ) stores the result of an expression in a variable, array element, or object property, as in these examples:

 x = 2; // stores the number 2 in the variable x month[11] = "December";// 12th element of the month array is "December" car.color = 0xFF0000; // color property of car object is 0xFF0000 (red) 

Ten compound assignment operators provide a concise notation for combining the assignment operator with various arithmetic or bitwise operators. For instance, the "add and reassign " operator ( += ) combines assignment and addition. Each compound assignment operator performs an operation on two operands and stores the result in the left operand. For instance, x += 2 adds 2 to x and stores the result in x . This is equivalent to x = x + 2 .

Table A.7 lists the compound assignment operators with examples and equivalent expressions.

Table A.7. Compound Assignment Operators

Name

Operator

Example

Equivalent Expression

add and reassign

+=

i += 2

i = i + 2

subtract and reassign

-=

balance -= debit

balance = balance - debit

multiply and reassign

*=

rate *= increase

rate = rate * increase

divide and reassign

/=

price /= discount

price = price / discount

modulo divide and reassign

%=

frameCounter % slowdown

frameCounter = frameCounter % slowdown

bit-shift left and reassign

<<=

answer <<= num

answer = answer << num

bit-shift right and reassign

>>=

result >>= 1

result = result >> 1

bitwise AND and reassign

&=

test &= 4

test = test & 4

bitwise XOR and reassign

^=

finalMask ^= initialMask

finalMask = finalMask ^ initialMask

bitwise OR and reassign

=

onOff = on

onOff = onOff on


The compound assignment operators are not more efficient computationally than their longer equivalents, nor do they result in smaller SWFs. They are strictly a notational convenience.

Note that

 x *= y + z 

is equivalent to

 x = x * (y + z) 

not

 x = x * y + z 

which is the same as

 x = (x * y) + z 



Using Macromedia Studio MX 2004
Special Edition Using Macromedia Studio MX 2004
ISBN: 0789730421
EAN: 2147483647
Year: N/A
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net