Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
package { import flash.display.Sprite; public class ClosureAS3 extends Sprite { public function ClosureAS3() { init() //giv-em-the-jit :) } public function init():void { //create a counter var counter1:Function = newCounter(); trace( counter1() ); //1 trace( counter1() ); //2 trace( counter1() ); //3 var counter2:Function = newCounter(); trace( counter2() ); //1 trace( counter2() ); //2 trace( counter1() ); //4 --> scope of i is still with counter1...cool! :) } public function newCounter():Function { var i:int = 0; //variable i gets bound into returned anonymous function via method Closure return function():int { //i is available to the scope of the anonymous function i=i+1; return i; } } } }
This paste will be private.
From the Design Piracy series on my blog: