13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Working with arrays<br />

The following example defines a function named clone() that does deep copying. The algorithm is borrowed from a<br />

common Java programming technique. The function creates a deep copy by serializing the array into an instance of<br />

the ByteArray class, and th<strong>en</strong> reading the array back into a new array. This function accepts an object so that it can be<br />

used with both indexed arrays and associative arrays, as shown in the following code:<br />

import flash.utils.ByteArray;<br />

function clone(source:Object):*<br />

{<br />

var myBA:ByteArray = new ByteArray();<br />

myBA.writeObject(source);<br />

myBA.position = 0;<br />

return(myBA.readObject());<br />

}<br />

Ext<strong>en</strong>ding the Array class<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

The Array class is one of the few core classes that is not final, which means that you can create your own subclass of<br />

Array. This section provides an example of how to create a subclass of Array and discusses some of the issues that can<br />

arise during the process.<br />

As m<strong>en</strong>tioned previously, arrays in ActionScript are not typed, but you can create a subclass of Array that accepts<br />

elem<strong>en</strong>ts of only a specific data type. The example in the following sections defines an Array subclass named<br />

TypedArray that limits its elem<strong>en</strong>ts to values of the data type specified in the first parameter. The TypedArray class is<br />

pres<strong>en</strong>ted merely as an example of how to ext<strong>en</strong>d the Array class and may not be suitable for production purposes for<br />

several reasons. First, type checking occurs at run time rather than at compile time. Second, wh<strong>en</strong> a TypedArray<br />

method <strong>en</strong>counters a mismatch, the mismatch is ignored and no exception is thrown, although the methods can be<br />

easily modified to throw exceptions. Third, the class cannot prev<strong>en</strong>t the use of the array access operator to insert values<br />

of any type into the array. Fourth, the coding style favors simplicity over performance optimization.<br />

Note: You can use the technique described here to create a typed array. However, a better approach is to use a Vector<br />

object. A Vector instance is a true typed array, and provides performance and other improvem<strong>en</strong>ts over the Array class<br />

or any subclass. The purpose of this discussion is to demonstrate how to create an Array subclass.<br />

Declaring the subclass<br />

Use the ext<strong>en</strong>ds keyword to indicate that a class is a subclass of Array. A subclass of Array should use the dynamic<br />

attribute, just as the Array class does. Otherwise, your subclass will not function properly.<br />

The following code shows the definition of the TypedArray class, which contains a constant to hold the data type, a<br />

constructor method, and the four methods that are capable of adding elem<strong>en</strong>ts to the array. The code for each method<br />

is omitted in this example, but is delineated and explained fully in the sections that follow:<br />

Last updated 6/6/2012<br />

43

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!