package
{
import flash.display.Sprite;
publicclassClosureAS3extendsSprite
{
publicfunctionClosureAS3()
{
init() //giv-em-the-jit :)
}
publicfunctioninit():void
{
//create a countervar counter1:Function= newCounter();
trace( counter1() ); //1trace( counter1() ); //2trace( counter1() ); //3var counter2:Function= newCounter();
trace( counter2() ); //1trace( counter2() ); //2trace( counter1() ); //4 --> scope of i is still with counter1...cool! :)
}
publicfunctionnewCounter():Function
{
var i:int=0; //variable i gets bound into returned anonymous function via method Closurereturn function():int
{
//i is available to the scope of the anonymous function
i=i+1;
return i;
}
}
}
}