Operators are symbols and keywords that change, access, create, remove, analyze, and organize data. Most operators come from the fields of mathematics and logic. A number of them are old friends from grade-school arithmetic. For instance, the multiplication operator ( * ) multiplies two values; the addition operator ( + ) adds two values; the division operator ( / ) divides one value by another. In some instances, an operator's form in ActionScript is different from that of grade-school arithmetic. For instance, the ActionScript multiplication operator is an asterisk ( *) , not x. Similarly, the division operator is a forward slash ( / ), not the · symbol. Although operators are very basic, they're also powerful and can be tricky. There are a lot of them ”52 in all, 51 of which are listed in Table 20.2. Forty operators fall into five major categories: -
Arithmetic operators operate on numeric operands to produce a numeric result. -
Assignment operators assign a result to a variable, object property, or array element; reassignment operators also include an arithmetic or bitwise operation with the assignment. -
Bitwise operators operate on individual bits within a byte. -
Comparison operators compare two values. -
Logical (Boolean) operators reduce expressions to Boolean values ( true or false ) and return results based on those values. In addition, 12 "one-of-a-kind" operators don't fall into any category: array-element/object-property, comma, conditional, delete , dot, function call, grouping, new , typeof , instanceof , void , and super . Table 20.2. ActionScript Operators Operator | Category | Usage | Precedence | Associativity | x++ | arithmetic | postfix increment | 16 | L-R | x-- | arithmetic | postfix decrement | 16 | L-R | . | N/A | object property access | 15 | L-R | [] | N/A | array-element/object-property | 15 | L-R | () | N/A | parentheses, grouping | 15 | L-R | function() | N/A | parentheses,function call | 15 | L-R | ++x | arithmetic | prefix increment | 14 | R-L | --x | arithmetic | prefix decrement | 14 | R-L | - | arithmetic | unary negation | 14 | R-L | ~ | bitwise | bitwise NOT | 14 | R-L | ! | logical | logical NOT | 14 | R-L | new | N/A | create object/array | 14 | R-L | delete | N/A | remove object/property/array element | 14 | R-L | typeof | N/A | determine datatype | 14 | R-L | void | N/A | return undefined value | 14 | R-L | * | arithmetic | multiply | 13 | L-R | / | arithmetic | divide | 13 | L-R | % | arithmetic | modulo divide | 13 | L-R | + | arithmetic string | add (number) concatenate (string) | 12 | L-R | - | arithmetic | subtract | 12 | L-R | << | bitwise | bitwise left shift | 11 | L-R | >> | bitwise | bitwise signed right shift | 11 | L-R | >>> | bitwise | bitwise unsigned right shift | 11 | L-R | < | comparison | less than | 10 | L-R | <= | comparison | less than or equal to | 10 | L-R | > | comparison | greater than | 10 | L-R | >= | comparison | greater than or equal to | 10 | L-R | instanceof | N/A | determine class | 10 | L-R | == | comparison | equality | 9 | L-R | != | comparison | inequality | 9 | L-R | === | comparison | strict equality | 9 | L-R | !== | comparison | strict inequality | 9 | L-R | & | bitwise | bitwise AND | 8 | L-R | ^ | bitwise | bitwise XOR | 7 | L-R | | bitwise | bitwise OR | 6 | L-R | && | logical | logical AND | 5 | L-R | | logical | logical OR | 4 | L-R | ?: | N/A | conditional | 3 | R-L | = | assignment | assignment | 2 | R-L | += | assignment | add and reassign | 2 | R-L | -= | assignment | subtract and reassign | 2 | R-L | *= | assignment | multiply and reassign | 2 | R-L | /= | assignment | divide and reassign | 2 | R-L | %= | assignment | modulo divideand reassign | 2 | R-L | <<= | bitwise | bit-shift left and reassign | 2 | R-L | >>= | bitwis | bit-shift right and reassign | 2 | R-L | >>>= | bitwise | bit-shift right (unsigned) and reassign | 2 | R-L | &= | bitwise | bitwise AND and reassign | 2 | R-L | ^= | bitwise | bitwise XOR and reassign | 2 | R-L | = | bitwise | bitwise OR and reassign | 2 | R-L | , | N/A | comma | 1 | L-R | Using Operators Table 20.3 illustrates some typical uses of operators. Table 20.3. Using Operators Statement | Operator(s) | Operator Name (s) | Operand(s) | Result | x = 3 + 7; | = + | Assignment, Addition | 3 , 7 | x is set to 10 | x = "Bed #"+6; | = + | Assignment, Concatenation | "Bed" , "6" | x is set to "Bed #6"; | x = a “ b; | = - | Assignment, Subtraction | a , b | x is set to a less b | x = 3 * 7; | = * | Assignment, Multiplication | 3 , 7 | x is set to 21 | x++; | ++ | Unary Increment | x | x is incremented by 1 | x--; | -- | Unary Decrement | x | x is decremented by 1 | A few definitions: -
Operands are what operators operate on. -
Operators combined with operands form expressions . An expression is a section of code that resolves to a single value. -
The statement is the smallest unit of ActionScript code that can actually cause something to happen. Often, all that's required to turn an expression into a statement is a semicolon. For instance, x++ is an expression, but x++; is a statement. (If you leave the semicolon off, the ActionScript interpreter tries to guess where it should go. But this is not a good practice. Officially, it's not a statement without the semicolon.) 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) The 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 that involves two operands. The result is stored 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 . Understanding Precedence, Associativity, and Operator Grouping When one statement includes multiple operators, the ActionScript interpreter has two simple rules by which it determines which operators to evaluate first: -
Each operator has a precedence value, as shown in Table 20.2. The interpreter evaluates operators in order of precedence, with higher precedence operators first. -
Operators with the same precedence are evaluated according to their associativity , either left to right (L “R) or right to left (R “L). Table 20.2 also shows the associativity of each operator. Examples of precedence include the following: 1 + 2 * 10 // 21 - multiplication happens before addition --6 * 100 // 500 - decrement happens before multiplication The following grouping operators , which are parentheses, allow you to override the default order of evaluation: (1 + 2) * 10 // 30 - addition happens before multiplication --(6 * 100) // 599 - multiplication happens before decrement You can also use parentheses to make the order of evaluation more obvious, even if they don't actually change anything. For instance: 1 + (2 * 10) // 21 doesn't really change anything Here's an example of associativity: The "add and reassign" ( += ) and "subtract and reassign" ( -= ) operators have the same precedence and right-to-left associativity. Therefore, in the expression a += b -= c , the interpreter starts by evaluating the operator on the right, so a += b -= c is the same as a += (b -= c) . For instance, in the following example, the interpreter evaluates b -= c first, making b equal to “1. Then it evaluates a += b , making a equal to 0, as shown here: a = 1; b = 2; c = 3; a += b -= c; // result: a == 0, b == -1, c == 3 For details of operators, see Appendix A, "ActionScript Reference," page 909 . |