Aug 07
method_missing in ActionScript 3/Flex
method_missing is one of the small bits of Ruby magic that can be used to some really amazing things when used properly, and some really dangerous things when used improperly. Rails dynamic finders are good example of the amazing things that you can do. Here are some others.
It is possible to implement something like method_missing in AS3 using the Proxy class. Here is a relatively simplistic example:
import flash.utils.Proxy; import flash.utils.flash_proxy; dynamic public class BaseProxy extends Proxy { flash_proxy override function callProperty(method: *, ...args): * { try { var clazz : Class = getDefinitionByName(getQualifiedClassName(this)) as Class; return clazz.prototype[method].apply(method, args); } catch (e : Error) { return methodMissing (method, args); } } protected function methodMissing(method : *, args : Array) : Object{ throw( new Error("Method Missing")); } }
The callProperty method is called whenever an undefined method is called on an instance of this class, or any instance of class that extends this class. In the try block we check if the method was defined on the prototype. If it is not found there, we call methodMissing.
So, now if we create another class that extends this one like so
public dynamic class Model extends BaseProxy { public function myMethod (arg1 : String, arg2 : Boolean) : String { return arg1 + " " + arg2; } }
and then run the following trace statements
import flash.display.Sprite; public class MethodMissingExample extends Sprite { public function MethodMissingExample() { var m : Model = new Model(); Model.prototype.runtimeMethod = function (date : Date) : String { return "I was defined at runtime at " + date.toLocaleTimeString(); }; trace(m.myMethod("I exist", true)); trace(m.runtimeMethod(new Date())); trace(m.someMethod(0, false, "x")); } }
You will see the following in the console:
I exist true I was defined at runtime at 09:58:00 PM Error: Method Missing
The first call succeeds because myMethod is defined. someMethod is not defined, so a Method Missing error gets thrown.
Now, if we override the missingMethod in the Model class like so,
public dynamic class Model extends BaseProxy { public function myMethod (arg1 : String, arg2 : Boolean) : String { return arg1 + " " + arg2; } override protected function methodMissing(method : *, args : Array) : Object { return "You called " + method + " with " + args.toString(); } }
and run the same trace statements you will see the following in the console:
I exist true I was defined at runtime at 09:58:00 PM You called someMethod with 0,false,x

August 7th, 2007 at 5:55 am
Cool.
But I believe that rails and the dynamic finders go further than that. In ruby, the proxy object is able to actually get the unmatched method defined on itself at runtime. For instance, in your example, the “methodMissing” did not have to be defined on the inherited Model, but “callProperty” would “simply” define it at runtime, when it catched a missing method. This way, it would not me missing the next time it was called.
Can actionscript (v3?) do that: Define new methods on objects at runtime, like ruby can?
August 7th, 2007 at 6:30 am
I’ve been using this a lot lately. You need to make sure you make the classes dynamic if you want to use the property aspect. Also I have yet to figure out the difference between flash_proxy and object_proxy, though every example I’ve seen uses flash_proxy.
August 8th, 2007 at 1:22 am
Hi Per –
You can definitely add new methods in AS3 at runtime. Check out this post for an example: http://flexonrails.net/?p=79
August 8th, 2007 at 2:15 am
Per – I went ahead and updated this post to show how you can add a method at runtime.
August 8th, 2007 at 2:27 am
Hi Brian – flash_proxy and object_proxy are both namespaces that are used to prevent name conflicts. All of the methods in the Proxy class are defined in the flash_proxy namespace, so if you want to override them you have to this namespace.
January 23rd, 2008 at 10:20 pm
I just wanted to chime in on this post….
In ActionScript 3, any entity that gets added to the Display List must actually extend flash.display.DisplayObject because the core Display List method signatures were not written to interfaces.
The Flex framework was built entirely on top of the core Display List base classes (rather than using Composition).
This means that in ActionScript 3, and especially in Flex projects, no visual entity can actually take advantage of this functionality.
Kind of disappointing.
January 24th, 2008 at 6:12 am
Hey Luke,
hmmm… that sounds like poor framework design to me. I suppose we could submit bug to the Flex bug tracker. Though it looks like it’s actually the underlying Flash Player method that expects a DisplayObject, so they may not be able to change this easily.
March 27th, 2008 at 8:39 pm
Intresting. Thanks
August 13th, 2008 at 8:53 pm
[...] was inspired to investigate this topic by Derek Wischusen’s post “method_missing in ActionScript.” I saw it and immediately set out to build a port of ActiveRecord to [...]
June 16th, 2009 at 4:40 am
[...] This great blog entry on flexonrails explains the magic. [...]