How can I pass all parameters (one by one) in an object to a constructor in AS3?

This is a hard question to do, but I'll try to explain. I have the Class and the parameters of its contructor as an object. What I need to do is a function that returns an instance of this class, passing this parameters to the constructor.

This is the code: Some random and unmodifiable class:

public Foo {
    public function Foo(a:int, b:String) {
        // constructor

And some function (in some another class):

function bar(params:Object):* {  
    var baz:Foo = new Foo(params.a, params.b);
    return baz;

What I need to do is make this function generic, without pass params as parameter to Foo constructor because I can't modify it. Something like:

function bar2(clazz:Class, params:Object):* {
    var baz:* = new clazz(/*some magic way to transform params in comma separated parameters*/);
    return baz;

Anyone can help me? Thanks a lot.


This is called parameterized factory. First I thought about Function.apply, but it doesn't apply to constructors (he-he). So, people are making factories like this:

function create(what:Class, args:Array):* {
    switch (args.length) {
        case 0: return new what();
        case 1: return new what(args[0]);
        case 2: return new what(args[0], args[1]);
    throw new Error("Need moar cases!");

Need Your Help

Echo once per foreach

php arrays hex

So I have the following code: