{"id":256,"date":"2009-04-17T22:03:06","date_gmt":"2009-04-18T05:03:06","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1177"},"modified":"2009-04-17T22:03:06","modified_gmt":"2009-04-18T05:03:06","slug":"object-argument-w-defaults","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=256","title":{"rendered":"Object Argument w\/ Defaults"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar boxDefaults:Object = {x:10, y:10, width:100, height:100, lineThickness:0, lineColor:0x000000, lineAlpha:1, fillColor:0xCCCCCC, fillAlpha:1}\r\nfunction drawBox(params:Object=null):void {\r\n\tvar p:Object=setDefaults(boxDefaults, params);\r\n\tgraphics.lineStyle(p.lineThickness, p.lineColor, p.lineAlpha);\r\n\tgraphics.beginFill(p.fillColor, p.fillAlpha);\r\n\tgraphics.drawRect(p.x, p.y, p.width, p.height);\r\n}\r\n\r\nfunction setDefaults(defaults:Object, params:Object=null):Object {\r\n\tif (params==null) {\r\n\t\tparams = new Object();\r\n\t}\r\n\tfor (var key:String in defaults) {\r\n\t\tif (params&#x5B;key]==null) {\r\n\t\t\tparams&#x5B;key]=defaults&#x5B;key];\r\n\t\t}\r\n\t}\r\n\treturn params;\r\n}\r\n\r\n\/\/ test it out... notice that all object properties are optional and have default values\t\t\t\t\t\t\t\t\t\t  \r\ndrawBox();\r\n\r\ndrawBox({x:200, y:200, lineColor:0xFF0000, lineThickness:2, fillColor:0xCCCC00});\r\n\r\ndrawBox({x:200, y:320, width:50, height:150, lineAlpha:0, fillColor:0x416CCF});\r\n<\/pre>\n<p>This is a technique I&#8217;ve been using recently&#8230; inspired by tweening engines. I find this to be suitable when a function or object constructor has lots and lots of arguments (80% of the time if I have a function or object constructor with too many arguments I re-think my design, but every now and then I use this technique). <\/p>\n<p>Basically, the function or object constructor takes one argument of type Object &#8211; once passed into the function this Object argument is passed to the setDefault() function which populates it with any properties that it&#8217;s missing &#8211; each property has a default value. As a result you end up with an easy to read function call with a variable number of arguments and well thought out default values.<\/p>\n<p>This snippet just draws a box (not very interesting or usefull) &#8211; I wanted to showcase the technique using a simple function. As a real world example&#8230; I recently used this technique in a mini-library I created for use with <a href=\"http:\/\/box2dflash.sourceforge.net\/\" target=\"blank\">Box2D<\/a>&#8230; will be posting the library in the next few days&#8230;. it&#8217;s a library for fast prototyping and custom rendering.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>var boxDefaults:Object = {x:10, y:10, width:100, height:100, lineThickness:0, lineColor:0x000000, lineAlpha:1, fillColor:0xCCCCCC, fillAlpha:1} function drawBox(params:Object=null):void { var p:Object=setDefaults(boxDefaults, params); graphics.lineStyle(p.lineThickness, p.lineColor, p.lineAlpha); graphics.beginFill(p.fillColor, p.fillAlpha); graphics.drawRect(p.x, p.y, p.width, p.height); } function setDefaults(defaults:Object, params:Object=null):Object { if (params==null) { params = new Object(); } for (var key:String in defaults) { if (params&#x5B;key]==null) { params&#x5B;key]=defaults&#x5B;key]; } } return params; } [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,17],"tags":[85,4],"class_list":["post-256","post","type-post","status-publish","format-standard","hentry","category-dynamic","category-functions","tag-acitonscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/256","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=256"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/256\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}