User Guide

Advanced Topics 247
TypedArray overridden methods
The TypedArray class overrides the four methods of the Array class that are capable of adding
elements to an array. In each case, the overridden method adds a type check that prevents the
addition of elements that are not the correct data type. Subsequently, each method calls the
superclass version of itself.
The
push() method iterates through the list of arguments with a for..in loop and does a
type check on each argument. Any argument that is not the correct type is removed from the
args array with the splice() method. After the for..in loop ends, the args array contains
values only of type
dataType. The superclass version of push() is then called with the
updated
args array, as the following code shows:
AS3 override function push(...args):uint
{
for (var i:* in args)
{
if (!(args[i] is dataType))
{
args.splice(i,1);
}
}
return (super.push.apply(this, args));
}
The concat() method creates a temporary TypedArray named passArgs to store the
arguments that pass the type check. This allows the reuse of the type check code that exists in
the
push() method. A for..in loop iterates through the args array, and calls push() on
each argument. Because
passArgs is typed as TypedArray, the TypedArray version of push()
is executed. The
concat() method then calls its own superclass version, as the following code
shows:
AS3 override function concat(...args):Array
{
var passArgs:TypedArray = new TypedArray(dataType);
for (var i:* in args)
{
// type check done in push()
passArgs.push(args[i]);
}
return (super.concat.apply(this, passArgs));
}