开发者

Flash AS3 ... (rest) parameter

OK, so we all know that using the ... (rest) parameter in a function开发者_JS百科 lets us pass any number of arguments, as in the example in the Adobe AS3 manual:

function traceArgArray(x: int, ... args) {

for (var i:uint = 0; i < args.length; i++) {

trace(args[i]);

}

} 

traceArgArray(1, 2, 3);

BUT, what I would love to be able to do is to have the option of EITHER passing individual arguments OR passing arguments from an already-existing array:

myArray:Array = new Array(1,2,3);

and so:

traceArgArray(myArray);

As I've written it here, the function treats myArray as a single object, so the output is 1,2,3--a representation of the array as a whole, not the individual contents of the array.

In order to transform myArray into a comma-delimited list that the rest operator would recognize as a list of individual elements, I tried this:

traceArgArray(myArray.join());

but that didn't change the output. It looks like in this case the argument is being interpreted as a single string rather than a comma-delimited list.

So, the puzzle I hope someone can help me with can be expressed in either of two ways:

Is it possible to get the rest parameter to treat an array argument as a comma-delimited list of arguments?

-or-

Is there a way I can transform an array into a comma-delimited list and then pass that list to the function AS a comma-delimited list (rather than a long string) so that the rest operator can interpret it correctly?

The basic thing I want to do--be able to pass arrays of values into functions that accept any number of arguments--would seem to be of general use, so perhaps there is a work-around I'm not seeing because I'm obsessing over the rest operator itself.

Thanks for reading!


What you're looking for is Function.apply(). In your case it would look like this:

myArray:Array = new Array(1,2,3);

function traceArgArray(x: int, ... args) {
    for (var i:uint = 0; i < args.length; i++) {
        trace(args[i]);
    }
} 

traceArgArray.apply(this, [0].concat(myArray)); // [0] being the first argument (x)


See this

AS3 ... (rest) parameter

and this is referenced in the above:

AS3 arguments

and this could be useful as well

filling in (...rest) parameters with an array?


I think in this case it is more about handling the args then trying to call the method different...

    traceArgArray(1, 2, 3);

    var myArray:Array = new Array(1,2,3);   
    traceArgArray(1,myArray,"hello");


    myArray.push(["a",23,"her", 1.456, ["--","||",12,11] ]);
    traceArgArray(1,myArray,"hello");

    private var theArgsILike:Array= new Array();

    private function traceArgArray(x: int, ... args):void {

    //Creates the args you expect...
        theArgsILike.push(x);
        addArgs(args);

    // Trace both old and new style args
        trace("NEW Call: ------");
        trace("\n \targs: ------");
        for (var i:uint = 0; i < args.length; i++) {    
            trace(args[i]);
        }
        trace("\n \t The Args I Like: ------");
        for ( i = 0; i < theArgsILike.length; i++) {
            trace(theArgsILike[i]);
        }
        trace("\n");

        theArgsILike = new Array();
    }

    //splitting each array in its parts to add them on theArgsILike
    private function addArgs(inArgs:Array):void{
            //check if it is array 
        for each(var arg in inArgs){
                    if( Class(getDefinitionByName(getQualifiedClassName(arg))) == Class(getDefinitionByName(getQualifiedClassName(inArgs)))  ){
                    //spilt iteratively
                        addArgs(arg);
                    }
                    else{
                    // add arg 
                        theArgsILike.push(arg);
                    }
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜