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.
You can grab the source files for this example here.