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

前端 未结 4 550
天命终不由人
天命终不由人 2020-12-10 17:35

I\'ve got a function wich can accept a varible number of parameter with a rest operator.

I want create an object passing the argument collected with the rest opera

4条回答
  •  再見小時候
    2020-12-10 18:05

    I'm searching for the answer too, but too sad to see the answer is no....

    Here's my current (not so good) way to do this kind of stuff, hope some of you interest:

    // 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");
    ...
    

提交回复
热议问题