Currying will help you !
Also, have a look at the Command pattern.
package {
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.utils.Dictionary;
import flash.display.Sprite;
public class Main extends Sprite {
private var dictionary : Dictionary;
public function Main() {
dictionary=new Dictionary();
var foo1:Foo=new Foo("John");
var foo2:Foo=new Foo("Jane");
dictionary[0]=curry(foo1,"Hello world");
dictionary[1]=curry(foo1,"My name is John");
dictionary[2]=curry(foo2,"Hello King Kong");
dictionary[3]=curry(foo2,"My name is Jane");
var timer:Timer=new Timer(1000,1);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerComplete);
timer.start();
function curry(foo:Foo,message:String) : Function {
var f : Function = function():void {
foo.traceIt(message);
};
return f;
}
}
private function timerComplete(e : TimerEvent) : void {
Timer(e.target).removeEventListener(e.type, arguments.callee);
dictionary[0]();//John trace : Hello world
dictionary[1]();//John trace : My name is John
dictionary[2]();//Jane trace : Hello King Kong
dictionary[3]();//Jane trace : My name is Jane
}
}
}
class Foo {
private var name : String;
public function Foo(name:String) {
this.name = name;
}
public function traceIt(message:String):void{
trace(name,"trace :",message);
}
}