'new' keyword

by 9re
♥0 | Line 71 | Modified 2010-02-11 12:32:14 | MIT License
play

ActionScript3 source code

/**
 * Copyright 9re ( http://wonderfl.net/user/9re )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/wHyX
 */

package {
	import flash.text.TextField;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
    		private var tf:TextField = new TextField;
    		private static var _trace:Function = trace;
    	
        public function FlashTest() {
            // write as3 code here..
            var p:Object = new point(3, 4);
            trace('p is ' + p);
            trace('the length of p is ' + p.getLength());
            trace(p.clone());
            p.normalize();
            trace('after normalizing: ' + p);
            new trace('new', 'trace');
            new trace(p = new fibb(20));
            new trace(new function ():Object {
            		return {
            			toString : function ():String {
            				return 'anonymus function';
            			}
            		}
            });
            
            
            tf.width = 465;
            tf.height = 465;
            addChild(tf);
            
            function fibb($length:int, $a:int = 1, $b:int = 1):Array {
            		var arr:Array = [$a, $b];
            		
            		arr.getItemAt = function (i:int):int {
            			if (i == 0) return $a;
            			if (i == 1) return $b;
            			
            			if (this[i]) return this[i];
            			
            			return (this[i] = this.getItemAt(i-1) + this.getItemAt(i-2));
            		};
            		arr.setLength = function (i:int):void {
            			if (i > 1) {
            				arr.getItemAt(i - 1);
            				arr.length = i;
            			}
            		};
            		
            		arr.setLength($length);
            		
            		
            		return arr;
            }
            
            
            function point(x:Number, y:Number):Object {
            		trace('new ' + x, y);
            		return {
            			x : x,
            			y : y,
            			getLength : function ():Number {
            				return Math.sqrt(this.x * this.x + this.y * this.y);
            			},
            			toString : function ():String {
            				return '[point x=' + this.x + ' y=' + this.y + ']';
            			},
            			clone : function ():Object {
            				return new point(this.x, this.y);
            			},
            			normalize : function ():void {
            				var l:Number = this.getLength();
            				this.x /= l;
            				this.y /= l;
            			}
            		};
            }
            
            function trace(...o:Array):void {
            		tf.appendText(o + '\n');
            		tf.scrollV = tf.maxScrollV;
            		_trace(o);
            }
        }
    }
}