Function Proxy for ActionScript 3
In all my AS2 work, I depend heavily on Person 13's Proxy Class, which allows you to create a reference to a function and pass in a dynamic number of arguments. This technique is very useful for setting up event handlers and callback functions in situations where you don't want to deal with the overhead of setting up an event listener architecture for your application.
As I'm transitioning to Flex, I've found that I just can't live without Proxy, so I've rewritten the code for AS3. For clarity, I've renamed it "FunctionProxy", since there is already a Proxy Class in AS3. Also, note the use of the ... (rest) parameter, which replaces the Function.arguments array from AS2.
The beauty of it is that it's now only 5 lines of code!
As I'm transitioning to Flex, I've found that I just can't live without Proxy, so I've rewritten the code for AS3. For clarity, I've renamed it "FunctionProxy", since there is already a Proxy Class in AS3. Also, note the use of the ... (rest) parameter, which replaces the Function.arguments array from AS2.
The beauty of it is that it's now only 5 lines of code!
package
{
public class FunctionProxy
{
public static function create(oTarget:Object, fFunction:Function, ... args):Function {
var fProxy:Function = function():void {
fFunction.apply(oTarget, args);
};
return fProxy;
}
}
}
//Usage
private function quack(num: int, s: String ): void{
trace("hello" + num + " " + s);
}
var f: Function;
f = FunctionProxy.create(this, quack, 1, "duck");
f();
//outputs "hello1 duck"