Adobe FLEX 2-PROGRAMMING ACTIONSCRIPT 3.0 Manual page 244

Programming actionscript 3.0
Table of Contents

Advertisement

As mentioned previously, arrays in ActionScript are not typed, but you can create a subclass of
Array that accepts elements of only a specific data type. The example in the following sections
defines an Array subclass named TypedArray that limits its elements to values of the data type
specified in the first parameter. The TypedArray class is presented merely as an example of
how to extend the Array class and may not be suitable for production purposes for several
reasons. First, type checking occurs at run time rather than at compile time. Second, when a
TypedArray method encounters a mismatch, the mismatch is ignored and no exception is
thrown, although the methods can be easily modified to throw exceptions. Third, the class
cannot prevent the use of the array access operator to insert values of any type into the array.
Fourth, the coding style favors simplicity over performance optimization.
Declaring the subclass
Use the
keyword to indicate that a class is a subclass of Array. A subclass of Array
extends
should use the
dynamic
not function properly.
The following code shows the definition of the TypedArray class, which contains a constant to
hold the data type, a constructor method, and the four methods that are capable of adding
elements to the array. The code for each method is omitted in this example, but is delineated
and explained fully in the sections that follow:
public dynamic class TypedArray extends Array
{
private const dataType:Class;
public function TypedArray(...args) {}
AS3 override function concat(...args):Array {}
AS3 override function push(...args):uint {}
AS3 override function splice(...args) {}
AS3 override function unshift(...args):uint {}
}
244
Working with Arrays
attribute, just as the Array class does. Otherwise, your subclass will

Advertisement

Table of Contents
loading

This manual is also suitable for:

Flex

Table of Contents