Run code before class instanciation in ActionScript 3
Posted
by soow.fr
on Stack Overflow
See other posts from Stack Overflow
or by soow.fr
Published on 2010-03-08T11:03:55Z
Indexed on
2010/03/08
11:06 UTC
Read the original article
Hit count: 209
I need to run code in a class declaration before its instanciation. This would be especially useful to automatically register classes in a factory. See:
// Main.as
public class Main extends Sprite
{
public function Main() : void
{
var o : Object = Factory.make(42);
}
}
// Factory.as
public class Factory
{
private static var _factory : Array = new Array();
public static function registerClass(id : uint, c : Class) : void
{
_factory[id] = function () : Object { return new c(); };
}
public static function make(id : uint) : Object
{
return _factory[id]();
}
}
// Foo.as
public class Foo
{
// Run this code before instanciating Foo!
Factory.registerClass(42, Foo);
}
AFAIK, the JIT machine for the ActionScript language won't let me do that since no reference to Foo
is made in the Main
method. The Foo
class being generated, I can't (and don't want to) register the classes in Main
: I'd like to register all the exported classes in a specific package (or library). Ideally, this would be done through package introspection, which doesn't exist in ActionScript 3.
Do you know any fix (or other solution) to my design issue?
© Stack Overflow or respective owner