Introduction
Objects
Instances
Local
Inheritance
Prototypes
Override
Overwrite
Protection
Arguments
Constructors
Methods
Proto
Arguments Array
Callee
New
Extend
Super
Glossary
 
 
Debreuil Digital Works © 2001
  The Arguments Array

Undocumented Feature!

It has to be said that the arguments array is an undocumented feature, and all that implies. While it is likely that it will still be around in future versions of Flash (It is fully supported in future versions of ECMAscript, and it allows functionality that can not be achieved in another way) there is no guarantee. If you do use it, only do so when you must. If Flash 6 comes out and it's a documented feature, then use it all you want. If Flash 6 comes out and it's gone, then you will have a minimum of code to change.

One of the things that makes class constructors and methods special is you can send them messages. Methods can even return a message (constructors return a new object, not a message). We covered this in the arguments section - the message is the arguments you send, and the return message comes via the return statement. Lets look a little closer at how constructors and methods handle these arguments.

Incoming arguments are actually stored in two places in actionscript. A new local property (local to the running activation object) is created for each name that is in the list of arguments between the parentheses (arg0, arg1). The other place they are stored is in an array called, what else, arguments (we'll refer to that as 'arguments[]', just to avoid confusion here!). You can access the passed values in this array by the order they were passed. So the first passed value goes into arguments[0], the second arguments[1]... Here is a quick example:

test = function( arg0, arg1 )
{
  trace( arg0 +" "+ arg1 );                // 1st 2nd
  trace( arguments[0] +" "+ arguments[1]); // 1st 2nd
}
test( "1st", "2nd" );

There are, as always, a few things to say about this. First, the most significant thing about this array is it allows you to pass an unknown number of arguments. If you have a method that calculates the average of numbers you send it (and you want to send it a different 'number of numbers' each time), you can use the arguments array - like this:

getAverage = function( _args_ ) {
  var total = 0;
  for(var i = 0; i < arguments.length; i++)
  {
    total += arguments[i];
  }
  return total / arguments.length;
}
trace( getAverage(4,5,3,2,6,10) ); // 5
trace( getAverage(54,45,96) );     // 65 

This is the main reason to use the arguments array, to pass an unknown number of parameters. Note that the _args_ word is not necessary, however it is always good to give yourself (or others reading your code) some indicator that 'hidden' arguments are being passed. This property will be dumped once the method is finished anyway, so it does some good and no real harm.

One surprising thing about the arguments array is that it isn't an array! You can get its length, like an array, but you can not use other array methods like 'pop( )' or 'push( )'. Another surprise, and perhaps a bug, is that the arguments[0] and the first argument name (arg0) are not the same object. So if you pass (arg0, arg1), and then change the value of arg0 to something new, arg0 and arguments[0] will now hold different values. This is unlike ECMA, so certainly don't count on this behavior (though expect it, or better yet, avoid it).

The last thing to know about the arguments array is that it has an extra property - callee. You can see it if you do a for-in trace on the arguments object. You will find callee is much easier to type than prototype, or __proto__, but there is a bit more to it than just this - as we'll see in the next section....

__proto__ < < Home > > Callee