# Functions as Object Review

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.

Here is a still of what it generates and below you'll find the timeline as code: Actionscript:
1. [SWF(width = 500, height=500)]
2.
3. var canvas:BitmapData = new BitmapData(800,800,false, 0x000000);
5. scaleX = scaleY = 500 / 800
6.
7.
8. var walkerNum:int = 50;
9. var walkers:Vector.<Function> = new Vector.<Function>(walkerNum, true);
10.
11. makeWalkers();
12. runWalkers();
13.
14. function makeWalkers():void{
15.     for (var i:int = 0; i<walkerNum; i++){
16.         walkers[i] = makeWalker();
17.     }
18. }
19. function runWalkers():void{
21. }
22. function onRun(evt:Event):void{
23.     for (var i:int = 0; i<walkerNum; i++){
24.         walkers[i]();
25.     }
26. }
27.
28. function makeWalker(xp:Number=400, yp:Number=400):Function{
29.     var x:Number = xp, y:Number = yp;
30.     var rad:Number = Math.random() * 4;
31.     var theta:Number = Math.random() * Math.PI * 2;
32.     var speed:Number = 0.01 * Math.random() * 2
33.     if (int(Math.random() * 2) == 1){
34.         speed *= -1;
35.     }
36.     return function():void{
37.         x += rad * Math.cos(theta);
38.         y += rad * Math.sin(theta);
39.         theta += speed
40.         if (int(Math.random() * 100) == 1){
41.             theta = Math.random() * Math.PI * 2;
42.         }
43.         if (x> 800 || x <0 || y> 800 || y <0){
44.             x = xp, y = yp;
45.         }
46.         canvas.setPixel(x, y, 0xFFFFFF);
47.     }
48. }

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 - it'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.

1. Lex
Posted March 11, 2010 at 7:19 pm | Permalink

This is soooo slick! I always use an Object Class that contains a property and then force that into an Array and loop though those. This cuts out that unnecessary step. Thanks for the insightt!

2. makc3d
Posted March 12, 2010 at 5:48 am | Permalink

I think I saw Higgs boson somewhere there.

3. Zevan
Posted March 12, 2010 at 8:11 am | Permalink

@makc3d ha yeah i thought it looked like an image from CERN a little bit 4. Philippe
Posted March 13, 2010 at 11:14 am | Permalink

Nice, but I guess it would be better to have canvas as a parameter of the anonymous function created by makeWalker.

5. Zevan
Posted March 13, 2010 at 1:25 pm | Permalink

not a bad idea at all… you can fork it on wonderfl if you like.

6. Phil
Posted March 26, 2010 at 1:52 am | Permalink

I love this man. There are some pretty nice things this effect could be used for and the code is sweet. great idea!

7. Zevan
Posted March 26, 2010 at 7:43 am | Permalink

thanks phil

• 