Need Flex Migration Help?
- Adobe ends Flash player support in 2020
- Flex apps will no longer function
- We can help migrate your app
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!