Arguments, a piece of history?

Did you use the arguments object in actionscript 2? This vague object that you could call without having seen it anywere. Well it has gotten a lot better with Actionscript 3. They introduced the rest keyword.

This is how it works:

Actionscript:
  1. //Create a function with the rest keyword like : "... args"
  2. public function average (name:String, ... args):void{
  3.   var total:Number = 0;
  4.  
  5.   //Handle the rest keyword like an array
  6.   for(var i:Number=0; i<args.length; i++){
  7.     total += args[i];
  8.   }
  9.  
  10.   var avarage:Number = total / args.length;
  11.  
  12.   trace("The average of " +name +" is: " +average);
  13.   // traces: The average of Averagetest is: 2.75
  14.  
  15. }
  16.  
  17. // Just call the function with a name and a variable number of numbers
  18. average("Averagetest", 2,2,3,4);

I even tried the arguments Object in actionscript 3 but it was kinda buggy. So I just dropped that, the rest keyword works fine and you don't have to guess where it is coming from. Bye bye arguments.


Post a Comment