Jun 13
Ruby-like Mixins in ActionScript 3
That title may be a bit of a stretch, AS3 does not support mixins in the same way as Ruby, but it is possible to define a set of methods and/or attributes in a single file and then mix those methods and attributes into your AS3 classes. Here’s how:
First create a new ActionScript file (a file, not a class) then add the methods and/or attributes that you want to mix in to your classes.
For example, I created a file called classmixin.as that looks like this:
// ActionScript file import flash.utils.*; // returns the name of the class public function whoAmI () : String { return getQualifiedClassName(this); } // returns the class object public function myClass () : Class { return getDefinitionByName(whoAmI()) as Class; } // returns the prototype object public function proto () : Object { return myClass().prototype; }
Now, let’s say that you want to add these methods directly to two classes, one is controller called ContactsController and one is a model called Contact. To do this you need to use the include statement along with the relative path to file that defines the methods. For example:
package com.rxr.mixinexample.control { public class ContactsController { include "/../classmixin/classmixin.as" } }
package com.rxr.mixinexample.model { public class Contact { include "/../classmixin/classmixin.as" } }
That’s it. Now both of these classes have all of the methods defined in classmixin.as, so if you were to run the following code:
package { import flash.display.Sprite; import com.rxr.mixinexample.control.ContactsController; import com.rxr.mixinexample.model.Contact; public class MixinExample extends Sprite { public function MixinExample() { var a : ContactsController = new ContactsController(); var b : Contact = new Contact(); trace(a.whoAmI()); trace(b.whoAmI()); } } }
you would see these trace statements in the console:
com.rxr.mixinexample.control::ContactsController
com.rxr.mixinexample.model::Contact
This mixin capability in AS3 works a bit differently than mixins in Ruby. In Ruby when you mixin a module you are really creating a reference to that module and when you call a method that was mixed in it is actually being delegated to the module. Also, in Ruby when you mixin in a module only the member methods get mixed in, though there is way to mixin statics as well, but you have to add some extra code to make this happen. This is because the module is an object itself and the static methods belong to the module, so if you want to add them to your class you need to extend the module.
In AS3 when you include a .as file, it is though you are appending that file directly to the class definition. So you are not creating a reference to a module, you are just adding the contents of the file to your class, so if you define static attributes or methods they will be added to your class along with the member attributes and methods.
Please let me know if you have any questions, or if any of this is unclear.

June 18th, 2007 at 4:49 am
Interesting, though don’t you think this makes your code sloppy and harder to manage? I can’t really see myself ever using this practice, or think of a situation in which to use it..
June 19th, 2007 at 2:05 am
[...] a previous post I discussed how you can use the include statement to ‘mixin’ methods and properties [...]
August 8th, 2007 at 11:04 am
Well, Ruben, I can think of a situation
A situation to avoid duplication, in this case. I have some properties, that need to be set in a lot of constructors in different classes, and I need to set to the same. I am unable to solve it with a superclass here. So, I define a mixin.as file with a lot of property assignments and simply include in all my constructors. Cool, as the property assignments is only at one place now. Maintainability has improved.
I myself have my doubts about mixins with respect to maintainability as a general case. But you can do some pretty nifty stuff with it!
And, thanks for post Derek!
November 30th, 2008 at 7:47 pm
I can think of a situation.
We’ve been using the .as file to “mixin” functionality to flex components. So instead of having actionscript and mxml in the same file, we’ve been using
So that includes the actionscript file. Well, we’ve used the same script in a couple of different files. At compile time, I think it actually just adds the code into the class, then compiles, but it keeps source redundancy down. I think I’ll start using this more.