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 />

AS3 override function concat(...args):Array<br />

{<br />

var passArgs:TypedArray = new TypedArray(dataType);<br />

for (var i:* in args)<br />

{<br />

// type check done in push()<br />

passArgs.push(args[i]);<br />

}<br />

return (super.concat.apply(this, passArgs));<br />

}<br />

The splice() method takes an arbitrary list of argum<strong>en</strong>ts, but the first two argum<strong>en</strong>ts always refer to an index<br />

number and the number of elem<strong>en</strong>ts to delete. This is why the overridd<strong>en</strong> splice() method does type checking only<br />

for args array elem<strong>en</strong>ts in index positions 2 or higher. One point of interest in the code is that there appears to be a<br />

recursive call to splice() inside the for loop, but this is not a recursive call because args is of type Array rather than<br />

TypedArray, which means that the call to args.splice() is a call to the superclass version of the method. After the<br />

for..in loop concludes, the args array contains only values of the correct type in index positions 2 or higher, and<br />

splice() calls its own superclass version, as shown in the following code:<br />

AS3 override function splice(...args):*<br />

{<br />

if (args.l<strong>en</strong>gth > 2)<br />

{<br />

for (var i:int=2; i< args.l<strong>en</strong>gth; i++)<br />

{<br />

if (!(args[i] is dataType))<br />

{<br />

args.splice(i,1);<br />

}<br />

}<br />

}<br />

return (super.splice.apply(this, args));<br />

}<br />

The unshift() method, which adds elem<strong>en</strong>ts to the beginning of an array, also accepts an arbitrary list of argum<strong>en</strong>ts.<br />

The overridd<strong>en</strong> unshift() method uses an algorithm very similar to that used by the push() method, as shown in<br />

the following example code:<br />

}<br />

AS3 override function unshift(...args):uint<br />

{<br />

for (var i:* in args)<br />

{<br />

if (!(args[i] is dataType))<br />

{<br />

args.splice(i,1);<br />

}<br />

}<br />

return (super.unshift.apply(this, args));<br />

}<br />

Last updated 6/6/2012<br />

47

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

Saved successfully!

Ooh no, something went wrong!