{"id":491,"date":"2010-03-10T19:12:49","date_gmt":"2010-03-11T02:12:49","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=2812"},"modified":"2010-03-10T19:12:49","modified_gmt":"2010-03-11T02:12:49","slug":"functions-as-object-review","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=491","title":{"rendered":"Functions as Object Review"},"content":{"rendered":"<p>Something I do a fair bit on this website is use functions as objects. Here is a simple example post on wonderfl. Timeline code and a brief description are available below.<\/p>\n<div style=\"text-align:center;width:465px;\"><iframe loading=\"lazy\" title=\"Function Use - wonderfl build flash online\" scrolling=\"no\" src=\"http:\/\/wonderfl.net\/blogparts\/7665969b515a154eeecdf1e56125966a56f75e85\" width=\"465\" height=\"490\" style=\"border:1px black solid;\"><\/iframe><a href=\"http:\/\/wonderfl.net\/code\/7665969b515a154eeecdf1e56125966a56f75e85\" title=\"Function Use - wonderfl build flash online\">Function Use &#8211; wonderfl build flash online<\/a><\/div>\n<p>Here is a still of what it generates and below you&#8217;ll find the timeline as code:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/actionsnippet.com\/wp-content\/function_walkers.gif\" \/><\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n&#x5B;SWF(width = 500, height=500)]\r\n\r\nvar canvas:BitmapData = new BitmapData(800,800,false, 0x000000);\r\naddChild(new Bitmap(canvas,&quot;auto&quot;,true));\r\nscaleX = scaleY = 500 \/ 800\r\n \r\n\r\nvar walkerNum:int = 50;\r\nvar walkers:Vector.&lt;Function&gt; = new Vector.&lt;Function&gt;(walkerNum, true);\r\n\r\nmakeWalkers();\r\nrunWalkers();\r\n\r\nfunction makeWalkers():void{\r\n\tfor (var i:int = 0; i&lt;walkerNum; i++){\r\n\t\twalkers&#x5B;i] = makeWalker();\r\n\t}\r\n}\r\nfunction runWalkers():void{\r\n\taddEventListener(Event.ENTER_FRAME, onRun);\r\n}\r\nfunction onRun(evt:Event):void{\r\n\tfor (var i:int = 0; i&lt;walkerNum; i++){\r\n\t\twalkers&#x5B;i]();\r\n\t}\r\n}\r\n\r\nfunction makeWalker(xp:Number=400, yp:Number=400):Function{\r\n\tvar x:Number = xp, y:Number = yp;\r\n\tvar rad:Number = Math.random() * 4;\r\n\tvar theta:Number = Math.random() * Math.PI * 2;\r\n\tvar speed:Number = 0.01 * Math.random() * 2\r\n\tif (int(Math.random() * 2) == 1){\r\n\t\tspeed *= -1;\r\n\t}\r\n\treturn function():void{\r\n\t\tx += rad * Math.cos(theta);\r\n\t\ty += rad * Math.sin(theta);\r\n\t\ttheta += speed\r\n\t\tif (int(Math.random() * 100) == 1){\r\n\t\t\ttheta = Math.random() * Math.PI * 2;\r\n\t\t}\r\n\t\tif (x &gt; 800 || x &lt; 0 || y &gt; 800 || y &lt; 0){\r\n\t\t\tx = xp, y = yp;\r\n\t\t}\r\n\t    canvas.setPixel(x, y, 0xFFFFFF);\r\n\t}\r\n}\r\n<\/pre>\n<p>The basic trick is to have a function return a function. The original function initializes typed local variables and the returned function has access to these values. This is one of many ways to do OOP style coding using functions alone. This is really only for fun and for speed coding reasons &#8211; it&#8217;s worth noting that this method is is significantly slower than using actual classes. I still have lots of fun with it despite the downsides.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Something I do a fair bit on this website is use functions as objects. Here is a simple example post on wonderfl. Timeline code and a brief description are available below. Function Use &#8211; wonderfl build flash online Here is a still of what it generates and below you&#8217;ll find the timeline as code: &#x5B;SWF(width [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24,17],"tags":[3,14,4],"class_list":["post-491","post","type-post","status-publish","format-standard","hentry","category-bitmapdata","category-functions","tag-actionscript","tag-as3","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/491","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=491"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/491\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}