9 Replies Latest reply on Aug 27, 2006 7:34 PM by NSurveyor

    new operator with Array for parameters?

    NSurveyor Level 2
      Hi, I was just curious if there was something similar to using Function.apply (to use an array as the parameters in a function call) for new constructor();

      Example, given: a=[1990,1,2], how do we get: new Date(1990,1,2);
      But note, in general, the length of a is unknown.

      Hope that makes a sense.
        • 1. Re: new operator with Array for parameters?
          injpix Level 3
          I am not really sure if I understand your question. However, will the code below help you?
          • 2. Re: new operator with Array for parameters?
            NSurveyor Level 2
            That's not what I meant.

            Given any array, a, and constructor, c, can we get:
            x = new c(a[0],a[1],a[2],...,a[a.length]);
            • 3. Re: new operator with Array for parameters?
              NSurveyor Level 2
              That a.length should be a.length-1
              • 4. Re: new operator with Array for parameters?
                abeall Level 3
                Interesting. There's nothing I know of. I assume you've tried using apply() on the class constructor?
                • 5. Re: new operator with Array for parameters?
                  NSurveyor Level 2
                  Yup, I've tried that already.

                  I'm doing this, because I'm trying to come up with a way to write ActionScript in an external document, and then have Flash load and excecute it. To make things easier, I'm using XML, but I can't get my head around making constructors work.

                  I could always simply have: new c(a[0],a[1],a[2],a[3],...,a[20])

                  and just hope that the number of parameters does not exceed 20 (which is pretty likely)
                  • 6. Re: new operator with Array for parameters?
                    abeall Level 3
                    Neat. I tried making an AS parser that wasn't XML based, it was completely string parsing, I didn't get very far :-)

                    Seems like there should be a way to do what you want, but I sure can't think of it. You're workaround seems fine though, I've used that for functions before I knew about Funcion.apply(). That won't work in AS3, though, because functions/constructors need to recieve the expected number of arguments or they throw an error.

                    Good luck, would love to see the final product if you feel like sharing!
                    • 7. Re: new operator with Array for parameters?
                      NSurveyor Level 2
                      I thought about this idea like a year ago, but I never actually tried it out. My XML based AS is going to be longer than the normal ActionScript, and slower.
                      The following does the equivalent of: req_txt.text = req_txt.text+'abc';

                      <script>
                      <set>
                      <str>req_txt.text</str>
                      <oper o='+'>
                      <get>req_txt.text</get>
                      <str>abc</str>
                      </oper>
                      </set>
                      </script>
                      • 8. Re: new operator with Array for parameters?
                        abeall Level 3
                        That's cool. One part that really tripped me up was handling conditionals. But I guess when using XML it's really just more of the same, with nodes blocked out for the different conditions.

                        BTW, I may misunderstand, but using apply() on the constructor seems to work in most cases:

                        import myClass
                        var args = [1,2,3,4];
                        var a = new myClass();
                        a.constructor.apply(a,args); //myClass constructor traces args

                        var num = new Number();
                        var args = [5];
                        num = num.constructor.apply(num,args);
                        trace(num)

                        I couldn't get it to work with BitmapData, partly because attempting to instantiate a BitmapData without args fails, and even if you put in 1,1 for width/height, the constructor.apply() doesn't seem to work.

                        var bmp = new flash.display.BitmapData();
                        var args = [mc._width,mc._height,true,0];
                        bmp.constructor.apply(bmp,args);
                        trace(bmp.width);
                        bmp.draw(mc);
                        this.attachBitmap(bmp,getNextHighestDepth());

                        Seems like there would be other problems, though, like constructors going off twice would likely cause problems with some classes, so your workaround seems much safer.
                        • 9. Re: new operator with Array for parameters?
                          NSurveyor Level 2
                          AH, I didn't try that.Thanks

                          For conditions/loops, try..catch..finally seems rather useful.