Named Operators

     

A number of operators are referred to by names instead of symbols. They include new , typeof , instanceof , delete , and void .

The new Operator

You use the new operator with a constructor function to create a datum of the object or array datatype. For instance, in the following examples, the constructor functions are Object() , Array() , and Date() :

 myObj = new Object(); myArray = new Array(); myDate = new Date(); 

The typeof Operator

The typeof operator returns a string indicating the datatype of an operand. Its format is

 typeof  expression  

where expression is any legal expression.

The instanceof Operator

The instanceof operator determines whether an object belongs to a class. Its format is as follows :

  object  instanceof  class  

The instanceof operator follows the inheritance chain, to determine whether the object inherits from the class. In the following example, for instance, a is an instance of Object because a is an array, which is an object, and all objects inherit from Object . On the other hand, a is not an instance of _global because _global is not a constructor function.

 a = new Array(); trace(a instanceof Array); // true trace(a instanceof Object); // true - this is true for all objects trace(a instanceof _global); // false - _global is not a constructor function 

In the following example, 6 is not an instance of Number because 6 is a primitive datum, not an object. On the other hand, n is an object whose value is 6 , and n is an instance of Number .

 trace(6 instanceof Number); // false -  6 is not an object n = new Number(6); trace(n instanceof Number); // true 

The delete Operator

The delete operator attempts to delete a variable, object (including a function), object property, array, or array element. The format is simply

 delete  identifier  

Here's an example of its use:

 myVar = "three"; // create a variable delete myVar; // remove it 

The delete operator returns true or false , depending on whether the deletion was successful. In the following example, delete returns false when you try to delete a property a second time:

 myObj = {a : "one", b: "two"} // create an object trace(delete myObj.a); // true trace(delete myObj.a); // false - it's already gone 

The void Operator

The ActionScript void operator appears to be present just for ECMA compatibility. Its format is

 void(  expression  ) 

It causes the interpreter to throw away the results of the expression and return undefined .

The Flash ActionScript Dictionary says that the void operator "is often used in comparisons using the == operator to test for undefined values." I can't find any real-life examples; however, the following does work:

 a = undefined; b = 1; trace(void(b) == a); // true 

Still, using the following approach is easier:

 a = undefined; trace(undefined == a); // true 

The Void datatype is used for strict typing, to indicate that a function doesn't return anything:

 function myFunc ():Void { // function body here }; 

However, this is Void as a datatype, not void as an operator.

Object Property Access: The Dot Operator

The dot ( . ) operator indicates a property of an object or a nested movie clip. For instance, you set a property to a value as follows:

 myObject.myProperty = "myValue"; // set a property to a string value 

Here, you set a variable, clipVar , equal to a movie clip instance name :

 clipVar = myClipParent.myClipChild; // set a variable to a movie clip name 

The Array-Element/Object Property Operator

Square brackets can indicate an element of an array or a property of an object. For instance, the following is an array element:

 months[0] = "January"; 

The following is an object property:

 computer["display"] = "SVGA"; 

The Parentheses/Function Call Operator

In addition to using parentheses for grouping other operators, you can use them as the function call operator, to invoke a function.

The format for calling , or invoking, a function is

  identifier  (  list  ) 

where identifier is the name of the function and list is an optional comma-separated list of arguments , or parameters, passed to the function. The operator here is the parentheses that enclose the arguments.



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