1 Reply Latest reply on Sep 17, 2010 7:48 PM by Mark Robbins 312

    Passing array of parameters to constructor

    skider Level 1

      I'm writing a script which facilitates interaction between javascript and an already existing flash library. One of the things I need to do is create an instance of class x, where class x can be one of a large number of classes. The constructors for these classes each take different numbers of parameters. I want to pass the parameters in from Javascript as an array, and I was hoping that there would be a way to pass the array contents into the constructor as parameters. I tried using apply, but it's not available on constructors.

       

      The only other option I know of is to write a large case statement full of constructors, and for each parameter pass an integer indexed array element. Is there any other way?

        • 1. Re: Passing array of parameters to constructor
          Mark Robbins 312

          Your going to have to unpack your array no matter what, so no.

          You can make your class factory with your huge case, or, if you have the source for your lib you could modify the constructor, but its the same amount of work:

           

          Was:

          public function MyClass(s:String, n:Number, d:Date){...}

           

          To:

          public function MyClass(s:*, n:Number=0, d:Date=nul){

            <case code here>

          }

           

          If you have a decent platform, or even javascript, you could maybe think about writing a parser that will generate the class factory case based on the constructor signatures.