actionscript 3 - How can I pass all parameters (one by one) in an object to a constructor in AS3? -
this hard question do, i'll try explain. have class
, parameters of contructor object. need function returns instance of class, passing parameters constructor.
this code:
random , unmodifiable class:
public foo { public function foo(a:int, b:string) { // constructor } }
and function (in class):
function bar(params:object):* { var baz:foo = new foo(params.a, params.b); return baz; }
what need make function generic, without pass params
parameter foo
constructor because can't modify it. like:
function bar2(clazz:class, params:object):* { var baz:* = new clazz(/*some magic way transform params in comma separated parameters*/); return baz; }
anyone can me?
lot.
this called parameterized factory. first thought function.apply
, doesn't apply constructors (he-he). so, people making factories 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]); ... //profit! } throw new error("need moar cases!"); }
Comments
Post a Comment