4

How can I achieve the following for any number of elements in the arg array? If it was a function, I'd use Function.apply(), but I can't figure out how to do it with the new operator.

var arg:Array = [1,2];
new MyClass( arg[0], arg[1] );
Community
  • 1
  • 1
Andy
  • 95
  • 1
  • 7
  • What are you trying achieve? Why not just pass the array as a separate argument? – rzetterberg Jul 28 '11 at 15:11
  • @Ancide: I've got an array and want to instantiate a class with those elements as arguments, without compromising type safety and compiler checks in other parts of the code. – Andy Jul 28 '11 at 15:18

4 Answers4

1

Dont pass each element of the array, just pass the array.

var arg:Array = [1,2];
new MyClass(arg);

Then inside of your class, loop through the array.

Localghost
  • 714
  • 3
  • 7
  • Then I'd need to edit the class constructor, which is being used in other places. I wouldn't want to compromise AS3's type security in those places because of this need. Also, I'd like a solution that works with classes that I can'd modify. – Andy Jul 28 '11 at 15:08
  • You can overload the class constructor. You can make one that accepts an array, and one that excepts an explicit list of values. This way, you can implement this without breaking previous invocations of this constructor. – Localghost Jul 28 '11 at 15:16
  • You can't overload any functions in ActionScript 3.0 – Allan Jul 29 '11 at 00:26
1

If you set up your class to accept a list of arguments using ... args you can pass in as many as you like. Then in the constructor you will access them just like a normal array.

class MyClass 
{
    public function MyClass(... args):void
    {
        //args is an Array containing all the properties sent to the constructor
        trace(args.length);
    }
}
shanethehat
  • 15,460
  • 11
  • 57
  • 87
  • 1
    I wish I could merge your answer and Jim's. And add a better explanation to how this will be invoked so there will not be a misunderstanding that passing one array will separate it into separate args. – rzetterberg Jul 28 '11 at 15:06
  • 1
    Yes. Andy, if you intend to only ever pass the full array, go with Jim's answer. If you intend to only pass some elements, or elements from different arrays that should be combined into a single array in `MyClass`, use my answer. – shanethehat Jul 28 '11 at 15:09
  • Then I'd need to edit the class constructor, which is being used in other places. I wouldn't want to compromise AS3's type security in those places because of this need. Also, I'd like a solution that works with classes that I can'd modify. – Andy Jul 28 '11 at 15:10
  • The constructor is mostly called "properly", but I'd also, on one point in the code, like to call it in a Function.apply()-like manner. It would not really be suitable to change the class constructor. – Andy Jul 28 '11 at 15:12
  • 1
    Undeclared overloading of a constructor is not permitted in AS3, so it will be necessary to modify the constructor of MyClass one way or another if you insist on passing this though the constructor. – shanethehat Jul 28 '11 at 15:13
  • @shane: Just like the [similar question](http://stackoverflow.com/questions/5260875/pass-arguments-from-an-array-to-an-actionscript-method-with-rest-argument) didn't require overloading, I suspect that there's a way to call the constructor indirectly. – Andy Jul 28 '11 at 15:16
  • I wish I could vote this question up more than once, and that I had more time to research it, but maybe this will help you: http://stackoverflow.com/questions/3874869/can-i-use-apply-with-constructor-to-pass-arbitrary-number-of-parameters/3936708#3936708 – shanethehat Jul 28 '11 at 15:25
1

It is unfortunately not possible, because there is no way to directly access the constructor method of a Class object.

Note: If you'd be using a Function object to make up your class (prototype inheritance), then it would be possible, but i figure, this is not an option for you.

You could work around the problem with a little (ugly) helper method, on which you can read about here: http://jacksondunstan.com/articles/398

ghost23
  • 2,150
  • 1
  • 20
  • 35
0

As stated in the comments is is not possible to apply settings on the constructor, but you could use this trick to set properties on a new instance of a class (which should be public)

public function setProps(o:Object, props:Object):* {
 for (var n:String in props) {
   o[n] = props[n];
 }
 return o;
}

.. use it like this

var args:Object = {x:1, y:2};
var instance:MyClass = setProps( new MyClass(), args ) );

source:
http://gskinner.com/blog/archives/2010/05/quick_way_to_se.html

Mark Knol
  • 9,663
  • 3
  • 29
  • 44