In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

クロージャってこういうこと?

Get Adobe Flash player
by paq 02 Dec 2009
    Embed
/**
 * Copyright paq ( http://wonderfl.net/user/paq )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/7of6
 */

package {
    import flash.display.Sprite;
    import flash.text.TextField;
    public class FlashTest extends Sprite {
        public function FlashTest() {
            // write as3 code here..
            var t:TextField = new TextField();
            t.height=465
            addChild(t)
 
            function closure():Function{
                var n:int = 0;
                return function():void{
                    n++;
                    t.appendText(n.toString()+"\n");
                }
            }
            var f:Function = closure();
            f();
            f();
            f();
            f();
            
            t.appendText("---------------\n");
            
            (function():Function{
                var n:int = 0;
                return function():Function{
                    n++;
                    t.appendText(n.toString()+"\n");
                    return arguments.callee
                }()
            })()()()()
            
            t.appendText("---------------\n");
            
            (function():Function{
                var n:int = 0;
                return function():Function{
                    n++;
                    t.appendText(n.toString()+"\n");
                    return function():Function{
                        n+=2;
                        t.appendText(n.toString()+"\n");
                        return function():Function{
                            n+=3;
                            t.appendText(n.toString()+"\n");
                            return function():Function{
                                n+=4;
                                t.appendText(n.toString()+"\n");
                                return function():void{
                                    n+=5;
                                    t.appendText(n.toString()+"\n");
                                }
                            }
                        }
                    }
                }()
            })()()()()()
        }
    }
}