Table of Contents

5.12 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.12.1 The typeof Operator

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

typeof operand

where operand can be any legal expression. The return value of the typeof operation is a string indicating the datatype of the evaluated operand. See Chapter 3 for more details.

5.12.2 The new Operator

The new operator creates a new composite datum�either an array or an object. The object can 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, Chapter 12, and the Constructor headings in the Language Reference.

5.12.3 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 (i.e., a variable, property, or element), the delete operation fails and returns the value false; otherwise, it returns true, indicating success. The delete operator is commonly used to remove an event handler, as follows:

// Add an event handler:
_root.onEnterFrame = function ( ) {
  trace("A frame passed");
}
   
// Remove an event handler:
delete _root.onEnterFrame;

See Chapter 10, Chapter 11, and Chapter 12 for details on the delete operator.

5.12.4 Array-Element/Object-Property Operator

As we'll see in Chapter 11 and Chapter 12, 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, nonnegative integer representing the index of the array element to access.

When accessing an object, the [ ] operator 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.12.5 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 cannot 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 left-hand 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 * 2;
newX = ball.xPosition + ball.xVelocity;

The dot operator is also used to invoke object methods (functions stored in properties), as follows:

intro_mc.gotoAndPlay("beginMovie");

See Chapter 12 and Chapter 13 for details on the dot operator.

5.12.6 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 for details on the conditional operator.

5.12.7 The instanceof Operator

The instanceof operator, added in Flash MX, checks whether an object is an instance of a given class (or one of the class's subclasses). It takes the form:

object instanceof classConstructor

where object is any object and classConstructor is a reference to any class's constructor function. The operation returns true if object is an instance of classConstructor or one of its subclasses; otherwise, it returns false. For example, the following code creates a Date object and then checks whether the object is an instance of the Date class:

// Create an instance of a built-in class.
var now = new Date( );
trace(now instanceof Date);  // Displays: true

The following code creates a Shape class with a Circle subclass and then checks whether a Circle object is an instance of Shape:

// Create Shape and Circle classes (constructor body code omitted)
function Shape ( ) {
  // ...
}
function Circle ( ) {
  // ...
}
// Establish inheritance: Circle is a subclass of Shape
Circle.prototype = new Shape( );
   
// Create a Circle object
var theCircle = new Circle( );
// Check the class of the Circle object using instanceof
trace(myCircle instanceof Circle);  // true
trace(myCircle instanceof Shape);   // true

The instanceof operator can be used to operate on an object conditionally, based on its class. For example, the following code uses instanceof to remove all text fields and movie clips from the current movie clip (this):

for (var p in this) {
  if (this[p] instanceof MovieClip) {
    this[p].removeMovieClip( );
  } else if (this[p] instanceof TextField) {
    this[p].removeTextField( );
  }
}

Note that the instanceof operator simply checks whether object's prototype chain includes classConstructor, as discussed in Chapter 12 under Section 12.7.

5.12.8 The super "Operator"

Added in Flash MX, super is an operator-like tool that serves two distinct object-oriented programming purposes:

In either case, super can be used only within a class constructor function or a method.

To invoke an object's superclass constructor function, use the following syntax:

super(arg1, arg2,...argn);

where arg1,...argn is a list of arguments passed to the superclass constructor function. Within the superclass constructor function, the this keyword points to the object whose class or method invoked the constructor. Used in this manner, the super operator is functionally equivalent to the following statement:

this.constructor.prototype.constructor.apply(this, arguments);

To invoke a superclass method, we use the following syntax:

super.methodName(arg1, arg2,...argn);

where methodName is some method of the superclass and arg1,...argn is a list of arguments passed to that method. Within the method, the this keyword points to the object whose class or method invoked the superclass method. Used in this manner, the super operator is functionally equivalent to the following rather unwieldy statement:

this.constructor.prototype._ _proto_ _.constructor.apply(this, arguments);

For practical examples showing super in use, see Section 12.5.2 and Section 12.5.3.2 in Chapter 12.

Note that super is not technically an operator. Rather, it is an identifier made available only during function execution, much like the arguments object. ActionScript's super is not an implementation of ECMAScript v4's super operator. Nevertheless, for the sake of discussion, both Macromedia's ActionScript Dictionary and this book categorize super with other operators.

5.12.9 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. If the function does not exist, the interpreter will not produce an error but merely fails silently. 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
theClip_mc.play( );              // Method of a movie clip, no arguments
myRectangle.area(6, 9);           // User-defined method, two arguments
init( );                          // User-defined function, no arguments

5.12.10 The Bitwise Operators

ActionScript supports a group of so-called bitwise operators that manipulate the binary digits (bits) of an integer. 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 online at:

http://www.moock.org/asdg/technotes/bitwise

Otherwise, use the Boolean logical operators (covered earlier in this chapter) instead. They perform the same tasks as the bitwise operators, albeit in a less optimized way. For quick reference, the bitwise operators (<<, >>, >>>, ~, &, ^, and | ) are included in Table 5-1.

See the Color.getRGB( ) method in the ActionScript Language Reference for an example that uses the bitwise operators to extract the red, green, and blue components from an RGB color triplet.


Table of Contents