{"id":287,"date":"2009-05-17T14:19:24","date_gmt":"2009-05-17T21:19:24","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1378"},"modified":"2009-05-17T14:19:24","modified_gmt":"2009-05-17T21:19:24","slug":"instantiate-and-set-properties","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=287","title":{"rendered":"Instantiate and Set Properties"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nfunction create(obj:Class, props:Object):*{\r\n\tvar o:* = new obj();\r\n\tfor (var p:String in props){\r\n\t\to&#x5B;p] = props&#x5B;p];\r\n\t}\r\n\treturn o;\r\n}\r\n\r\n\/\/ test out the function\r\n\r\nvar txt:TextField = create(TextField, {x:200, y:100, selectable:false, text:&quot;hello there&quot;, textColor:0xFF0000, defaultTextFormat:new TextFormat(&quot;_sans&quot;, 20)});\r\naddChild(txt);\r\n\r\nvar s:Sprite = Sprite(addChild(create(Sprite, {x:100, y:100, rotation:45, alpha:.5})));\r\n\r\nwith (s.graphics) beginFill(0xFF0000), drawRect(-20,-20,40,40);\r\n\r\nvar blur:BlurFilter = create(BlurFilter, {blurX:2, blurY:8, quality:1});\r\n\r\ns.filters = &#x5B;blur];\r\n<\/pre>\n<p>This snippet shows a function called create() that takes two arguments. The first argument is the name of a class to instantiate. The second is an Object with a list of properties to set on a newly created instance of the class (referenced in the first argument). <\/p>\n<p>This could be particularly useful for TextFields which for some reason have no arguments in their constructor.<\/p>\n<p>This will currently only work for classes that have either all optional constructor arguments or no constructor arguments. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>function create(obj:Class, props:Object):*{ var o:* = new obj(); for (var p:String in props){ o&#x5B;p] = props&#x5B;p]; } return o; } \/\/ test out the function var txt:TextField = create(TextField, {x:200, y:100, selectable:false, text:&quot;hello there&quot;, textColor:0xFF0000, defaultTextFormat:new TextFormat(&quot;_sans&quot;, 20)}); addChild(txt); var s:Sprite = Sprite(addChild(create(Sprite, {x:100, y:100, rotation:45, alpha:.5}))); with (s.graphics) beginFill(0xFF0000), drawRect(-20,-20,40,40); var blur:BlurFilter = create(BlurFilter, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,17,37,10],"tags":[3,4],"class_list":["post-287","post","type-post","status-publish","format-standard","hentry","category-dynamic","category-functions","category-one-liners","category-properties","tag-actionscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=287"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/287\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}