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

Function.applyの使い方

こういうことでいいのかな
Get Adobe Flash player
by ton 03 Jun 2009
/**
 * Copyright ton ( http://wonderfl.net/user/ton )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/tflJ
 */

//こういうことでいいのかな
package {
    import flash.display.*;
    import flash.text.TextField;
    public class FlashTest extends Sprite {
        public function FlashTest() {
            var txt:TextField = new TextField();
            txt.autoSize = "left";
            addChild(txt);

            
            /////////////////////////////
            //第1引数の使い方
            /////////////////////////////
            txt.appendText("第1引数\n");      
                  
            var sprite:Sprite = new Sprite();
            var mc:MovieClip = new MovieClip();
            
            var func:Function = function():void{
               txt.appendText(this.toString()+"\n");
            }
            
            func.apply(sprite);
            func.apply(mc);
            func();          
            
            /////////////////////////////
            //第2引数の使い方
            /////////////////////////////
            txt.appendText("第2引数\n");
            
            function average(val1:Number, val2:Number, ... rest):Number {
                var sum:Number = val1 + val2;
                for each(var n:Number in rest) sum += n;
                return sum / (2 + rest.length);
            }
            
            var list:Array = [1,2,3,4,5,6,7,8,9,10];
            
            var n:Number = average.apply(null, list);
            
            txt.appendText(n.toString());
                            
        }
    }
}