package { import fl.controls.Button; import fl.controls.TextArea; import flash.display.Sprite; public class test extends Sprite { private var b1:Button; private var b2:Button; private var b3:Button; private var ta:TextArea; public function test() { testAddField(); testAddMethod(); } //动态添加属性 public function testAddField():void{ var obj:Object = {}; obj.name = "hello"; obj.value = "world"; trace(obj.name); //输出hello trace(obj.value); //输出world } //动态添加方法 public function testAddMethod():void{ var obj:Object = {}; obj.hello=function(){ trace("hello,world"); } obj.hello(); } } }
输出:
hello world hello,world [SWF] C:/Documents and Settings/my/Adobe Flash Builder Beta 2/test/bin-debug/test.swf - 解压缩后为 80,089 个字节