5.1 General Features of Operators

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 5.  Operators

Though each operator has its own specialized task, all operators share a number of general characteristics. Before we consider the operators individually, let's see how they behave generally.

5.1.1 Operators and Expressions

Operators perform actions using the data values (operands) supplied. For example, in the operation 5 * 6, the numbers 5 and 6 are the operands of the multiplication operator (*). The operands can be any kind of expression; for example:

total = player1score + bonusScore;             // Operands are variables answer = (x+y) - (Math.PI * radius * radius);    // Operands are complex expressions

Observe in the second example that both the left and right operands of the - operator are expressions that themselves involve other operations. We can use complex expressions to create even larger expressions, such as:

answer =((x+y) - (Math.PI * radius * radius)) / 2  // Divide the whole thing by 2

When expressions become very large, consider using variables to hold interim results for both convenience and clarity. Remember to name your variables descriptively, such as:

var radius = 10; var height = 25; var circleArea = (Math.PI * radius * radius); var cylinderVolume = circleArea * height;

5.1.2 Number of Operands

Operators are sometimes categorized according to how many operands they take (i.e., require or operate on). Some ActionScript operators take one operand, some take two, and one even takes three:

-x                                           // One operand x * y                                        // Two operands (x =  = y) ? "true result" : "false result"  // Three operands

Single-operand operators are called unary operators; operators that take two operands are called binary operators; operators that take three operands are called ternary operators. For our purposes, we'll look at operators according to what they do, not the number of operands they take.

5.1.3 Operator Precedence

Operators' precedence determines which operation is performed first in an expression with multiple operators. For example, when multiplication and addition occur in the same expression, multiplication is performed first:

4 + 5 * 6  // Yields 34, because 4 + 30 = 34

The expression 4 + 5 * 6 is evaluated as 4 + (5 * 6) because the * operator has higher precedence than the + operator. Similarly, when less-than (<) and concatenation (+) operators occur in the same expression, concatenation occurs first. For example, this statement displays false in the Output window (which is a surprise if you don't know the precedence of the < and + operators):

trace("result: " + "a" < "b");

First, "result: " is joined with "a", creating the new string "result: a". This new string is then compared with "b", which yields false. Parentheses should be added to force the intended order of operation, as follows:

trace("result: " + ("a" < "b"));  // Displays: result: true

When in doubt, or to ensure a different order of operation, use parentheses, which have the highest precedence:

(4 + 5) * 6  // Yields 54, because 9 * 6 = 54

Even if not strictly necessary, parentheses can make a complicated expression more readable. The expression:

// x is greater than y, or y equals z x > y || y =  = z

may be difficult to comprehend without consulting a precedence table. It's a lot easier to read with parentheses added:

(x > y) || (y =  = z)  // Much better!

Table 5-1 shows the precedence of each operator. Operators with the highest precedence (at the top of the table) are executed first. Operators with the same precedence are performed in the order they appear in the expression, usually from left to right, unless the associativity is right to left (see the Associativity column in Table 5-1). See Section 5.1.4 for details.

Table 5-1. ActionScript operator associativity and precedence

Operator

Precedence

Associativity

Description

.

15

left to right

object property access

[ ]

15

left to right

array element access

()

15

left to right

parentheses

function()

15

left to right

function call

x++

14

left to right

postfix increment

x--

14

left to right

postfix decrement

++x

14

right to left

prefix increment

--x

14

right to left

prefix decrement

-

14

right to left

unary negation

~

14

right to left

bitwise NOT

!

14

right to left

logical NOT

new

14

right to left

create object/array

delete

14

right to left

remove object/property/array element

typeof

14

right to left

determine datatype

void

14

right to left

return undefined value

*

13

left to right

multiply

/

13

left to right

divide

%

13

left to right

modulo division

+

12

left to right

addition or string concatenation

-

12

left to right

subtraction

<<

11

left to right

bitwise left shift

>>

11

left to right

bitwise signed right shift

>>>

11

left to right

bitwise unsigned right shift

<

10

left to right

less than

<=

10

left to right

less than or equal to

>

10

left to right

greater than

>=

10

left to right

greater than or equal to

instanceof

10

left to right

check an object's class

= =

9

left to right

equality

!=

9

left to right

not equal to

= = =

9

left to right

strict equality

!= =

9

left to right

strict inequality

&

8

left to right

bitwise AND

^

7

left to right

bitwise XOR

|

6

left to right

bitwise OR

&&

5

left to right

logical AND

||

4

left to right

logical OR

?:

3

right to left

conditional

=

2

right to left

assignment

+=

2

right to left

add and reassign

-=

2

right to left

subtract and reassign

*=

2

right to left

multiply and reassign

/=

2

right to left

divide and reassign

%=

2

right to left

modulo division and reassign

<<=

2

right to left

bit-shift left and reassign

>>=

2

right to left

bit-shift right and reassign

>>>=

2

right to left

bit-shift right (unsigned) and reassign

&=

2

right to left

bitwise AND and reassign

^=

2

right to left

bitwise XOR and reassign

|=

2

right to left

bitwise OR and reassign

,

1

left to right

comma

5.1.4 Operator Associativity

As we've just seen, operator precedence indicates the pecking order of operators: those with a higher precedence are executed before those with a lower precedence. But what happens when multiple operators occur together and have the same level of precedence? In such a case, we apply the rules of operator associativity, which indicate the direction of an operation. Operators are either left-associative (performed left to right) or right-associative (performed right to left). For example, consider this expression:

a = b * c / d

The * and / operators are left-associative, so the * operation on the left (b * c) is performed first. The preceding example is equivalent to:

a = (b * c) / d

In contrast, the = (assignment) operator is right-associative, so the expression:

a = b = c = d

says, "assign d to c, then assign c to b, then assign b to a," as in:

a = (b = (c = d))

Operator associativity is fairly intuitive, but if you're getting an unexpected value from a complex expression, consult Table 5-1 or add extra parentheses. We'll note cases in which associativity is a common source of errors throughout the remainder of this chapter.

5.1.5 Datatypes and Operators

Some operators accept varying datatypes as operands. Depending on the datatype of an operand, the effect of an operator may change. The + operator, for example, performs addition when used with numeric operands, but it performs concatenation when used with string operands. If operands are of different datatypes or of the wrong type, ActionScript will perform type conversion according to the rules described in Chapter 3, which can have serious effects on your code.

The remainder of this chapter offers reference-style descriptions of each ActionScript operator. Newer programmers may wish to skim past topics that have not yet been covered in detail.

     



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

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