Can I use apply () with a constructor to pass an arbitrary number of parameters

I have a function that can take a variable number of parameter with a rest operator.

I want to create an object that passes the argument assembled with the rest statement directly to the constructor without creating the object and call the initialization function and without passing the entire array, but I do the ah parameters using the apply () function.

Is it possible? Using the application does not work.

public function myFunc(...arg) { // something link "new MyClass.apply(args)" return new MyClass(); } 
+6
constructor new-operator flash actionscript-3 apply
source share
4 answers

Unfortunately not. Unable to apply work to constructor. What is usually done to prepare the call number based on the number of arguments:

 public function myFunc(...arg):Myclass { switch (arg.length) { case 0:return new MyClass(); case 1:return new MyClass(arg[0]); case 2:return new MyClass(arg[0], arg[1]); //... etc case n:return new MyClass(arg[0], arg[1],..,arg[n]); default: throw new Error("too much arguments in myFunc"); } } 
+14
source share

Well, that led me to an interesting lengthy study!

I found this neat SWC file filled with utils to simulate AS2 eval (): http://www.riaone.com/products/deval/index.html

And here is the proof that what you are looking for can really work:

 package tests { import flash.display.BitmapData; import flash.display.Sprite; import flash.utils.getQualifiedClassName; import r1.deval.D; public class RandomTests extends Sprite{ public function RandomTests() { super(); var test:BitmapData = create(BitmapData, 100, 100, true, 0x00000000); trace(test); } public function create( pClass:Class, ... pArgs ):* { D.importClass(pClass); var fullQName:String = getQualifiedClassName(pClass); var qNameSplit:Array = fullQName.split("::"); var className:String = qNameSplit[1]; fullQName = qNameSplit.join("."); var statements:String = "import $0;\n" + "return new $1($2);"; var args:Array = []; for (var a:int = 0, aLen:int = pArgs.length; a < aLen; a++) { switch(pArgs[a].constructor) { case String: args[a] = "\"" + pArgs[a] + "\""; break; default: args[a] = pArgs[a]; break; //throw new Error("Unhandled type, please add it: " + pArgs[a].constructor); } } return D.eval(XString.gsub(statements,[fullQName, className, args.join(",")])); } } } 

Sorry for the dependency bits (like my XString class for simple subtasks), but it works theoretically. The only problem would be to pass references to objects as argument records. But then again ... the class r1.deval.D could take it ... hmm.

In any case, I thought, perhaps it would be worth sharing.

+2
source share

good also it

 public function myFunc(args:Object) { //then access various argumens return new MyClass(args.name, args.id, args.active) } 

and name it via myFunc({id:33,name:'jo')

could you pass the object, or is it too far from what you are looking for?

0
source share

I am also looking for an answer, but too sad to see the answer is not ....

Here is my current (not very good) way to do this stuff, hope some of you are interested in:

 // Foo.as public class Foo { // construct public function Foo(... args):void { create.apply(this, args); } // this function do as a really construct function, tricky stuff function create(id:uint, name:String) { trace(id, name); } } // Bar.as // for create this kind of class, just new it as usual ... var foo:Foo = new Foo(123, "abc"); ... 
0
source share

All Articles