start page | rating of books | rating of authors | reviews | copyrights

Book HomeActionScript: The Definitive GuideSearch this book

5.11. Other Operators

The remaining operators apply to topics considered in other chapters. We'll include them here for quick reference only and describe their usage fully in those chapters.

5.11.1. The Bitwise Operators

If you're planning to develop large-scale systems in which every iota of memory, calculation speed, and transfer-rate optimization makes a meaningful difference in performance, read about the bitwise operators in Chapter 15, "Advanced Topics". Otherwise, use the Boolean logical operators, which perform the same tasks the bitwise operators do, albeit in a less optimized way.

5.11.2. The typeof Operator

The typeof operator is used to determine the datatype of an expression. It takes one operand, as follows:

typeof operand;

where operand may be any legal expression. The return value of the typeof operation is a string indicating the datatype of the evaluated operand. See Chapter 3, "Data and Datatypes" for more details.

5.11.3. The new Operator

The new operator creates a new composite datum -- either an array or an object. The object may be a member of a built-in class or a user-defined class. The syntax for new is:

new constructor

where constructor must be a function that defines the properties of the newly created object. See Chapter 11, "Arrays", and Chapter 12, "Objects and Classes".

5.11.4. The delete Operator

We use the delete operator to remove an object, an object property, an array element, or variables from a script. The syntax for delete is:

delete identifier  

If identifier is not a data container (variable, property, or element), the delete operation fails and returns the value false; otherwise, it returns true, indicating success. See Chapter 11, "Arrays".

5.11.5. Array-Element/Object-Property Operator

As we'll see in Chapter 11, "Arrays" and Chapter 12, "Objects and Classes", we use the [ ] operator to retrieve and set the value of an array element or an object property. When accessing an array it takes the form:

array[element]

where array is the name of an array or an array literal and element is an expression that resolves to a zero-relative, non-negative integer representing the index of the array element to access.

When accessing an object, it takes the form:

object[property]

where object is an object name or object literal and property is any expression that resolves to a string representing the name of the object property to access.

When used on the left side of an assignment operator (=), the element or property is assigned the new value shown on the right side of the expression:

var colors = new Array( );  // Create a new array
colors[0] = "orange";      // Set its first element
colors[1] = "green";       // Set its second element

var ball = new Object( );   // Create a new object
var myProp = "xVelocity";  // Store a string in a variable
ball["radius"] = 150;      // Set the radius property
ball[myProp] = 10;         // Set the xVelocity property through myProp

When used anywhere else, the expression returns the value of the specified element or property:

diameter = ball["radius"] * 2;  // Sets diameter to 300
trace(colors[0]);               // Displays "orange"

5.11.6. The dot Operator

The dot operator is our primary means of referring to object properties and nested movie clips. Functionally, the dot operator has the exact same purpose as the [] operator -- it lets us set and retrieve object-property values. But the two operators have syntactic differences that make them unique. The general syntax of the dot operator is:

object.property

where object must be the name of an object or an object literal, and property must be an identifier that represents a property of object. Note that property may not be an arbitrary expression or a string literal; it must be the name of a property. Because array elements are numbered, not named, the dot operator cannot be used to access the elements of an array.

When used as the lefthand operand of an assignment operator, the dot operator is used to set a new value for a property:

var ball = new Object( );
ball.radius = 150;
ball.xVelocity = 10;

When used anywhere else, a dot operation returns the value of the named object property:

diameter = ball.radius;
newX = ball.xPosition + ball.xVelocity;

See Chapter 12, "Objects and Classes" and Chapter 13, "Movie Clips".

5.11.7. The conditional Operator

The conditional operator is a syntactic convenience that lets us succinctly represent a simple conditional statement. This operator takes three operands, in the form:

condition ? result_if_true : result_if_false;

When a conditional operation is executed, the first operand (condition) is evaluated. If condition is true or can be converted to true, the value of the second operand (result_if_true) is returned. Otherwise, the value of the third operand (result_if_false) is returned. See Chapter 7, "Conditionals".

5.11.8. The Function Call Operator

As we've seen with the trace( ) function and the string-manipulation functions, we use the function call operator, ( ), to invoke a function. A function call takes the general form:

function_name(argument_list)

The first operand, function_name, is the name of some function and must be an identifier, not an expression. The function must exist or the interpreter will produce an error. The argument_list is a series of zero or more arguments passed to the function, separated by commas. The return value of a function call operation is the return value supplied by the function itself.

The function call operator can call any built-in or user-defined function:

trace("an internal function");  // Built-in function, one argument
myClip.play( );                  // Method of a movie clip, no arguments
myRectangle.area(6, 9);         // User-defined method, two arguments
init( );                         // User-defined function, no arguments


Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.