The First and Only Magazine for Macromedia MX™
   
 
Notes from the Flash, Flex and ColdFusion trenches

Category List

Quick Poll

Where are you located?
North America
South America
Europe
Asia
Africa
Australia

My RSS Feeds








Hit Counter

Total: 1,305,972
since: 20 Jan 2005

Search Box

 

Search The Web

Google

MAX 2008

Fun Stuff

Mailing List

Got A Question?

Got A Question?

Leave a comment by the appropriate Entry, or email me

Tag Cloud

                                                                                                                       

Using an array as a list of arguments

posted Friday, 22 April 2005

Did you know you can take a standard Array in ActionScript and pass it to a function and have each element of the array treated as an individual argument?  The trick to making this work lies in the .apply method of the Function class.  Actually, getting ones head around the idea that functions are an instance of a class is a bit of a conceptual leap for some.

I ran across this need recently when I was building a class to act as a facade to another class, essentially, developers would call my facade class, which would take the data, figure out how to implement the request, add a few new arguments and pass the request on to another class.  One of the challenges I had was not knowing the total number of arguments which would be passed to the facade.  Regardless how many there were, I wanted to pass them all plus a new first argument to a method of the other class, here is how i did it.

function facadeToOtherClass(){
    var args:Array = arguments
    args.unshift("new first element");
    instanceOfOtherClass.otherMethod.apply(instanceOfOtherClass,args);
}

the apply method of the Function class takes two arguments, the first is the object which contains the function (the object whose method you are calling), the second is an array.  Each element in the array will be received in otherMethod as an individual argument!

tags:    

links: digg this    del.icio.us    technorati    reddit