views:

1225

answers:

3

Hello,

I've tested the following code:

function aa(...aArgs):void
{
    trace("aa:", aArgs.length);
    bb(aArgs);
}
function bb(...bArgs):void
{
    trace("bb:", bArgs.length);
}
aa(); //calling aa without any arguments.

The output is:

aa: 0 //this is expected.
bb: 1 //this is not!

When I pass empty arguments (aArgs) to bb function; shouldn't it return 0 length? Seems like function bb is treating the passed aArgs as non-empty / non-null..

What am I missing here?

Any help is appreciated. regards..

+3  A: 

It looks like aArgs going to the bb() function would be an empty array, but an array none the less... I would say that output is to be expected. I'm not really sure though how I would format it differently though to get the desired output...

Update 1:

I wanted to clarify a little bit. What you have is basically the same thing as:

function aa(...aArgs):void
{
    myArray:Array = aArgs;
    bb(myArray);
}
function bb(...bArgs):void
{
    trace("bb:", bArgs.length);
}
aa(); //calling aa without any arguments.

If you saw this code, you would expect bb:1 yes?

Update 2:

This thread: http://stackoverflow.com/questions/636853/filling-in-rest-parameters-with-an-array looks as though it would be relevant. It uses the apply() function to pass in an array as an parameter list. http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/Function.html#apply()

Ryan Guill
I see. thanks. calling bb.apply(null, args) in the aa function will sure work in this example. (I would vote up your answer if I had the 15 rep.)But actually, I'm trying pass the ...args to a variable.Such as: function aa(...args) { someVariable = args; } and the if I later use that variable in bb(...args) function like: bb(someVariable); -- it won't work..
radgar
That should still work with the apply. just remember that the somveVariable is an array and so you will need apply() to be able to pass the individual items in teh array along.
Ryan Guill
No it does not.. Please see the thread at http://stackoverflow.com/questions/972460/as3-arguments I cannot add code here so I had to ask a new question..
radgar
You can update the original question with code, no?
Ryan Guill
+1  A: 

This makes perfect sense, and working properly. ...rest always creates an Array, if there are no values passed in it creates an empty Array, as you see by tracing its length. So the reason why bb has one object in its ...rest array is that you are passing the empty array into bb as a value, which gets inserted into the first position of the Array generate by bb's ...rest, giving it a length of one.

Tyler Egeto
Yes I agree with all of the above. But it's just describing the nature of the problem.. not providing us the proper solution, yet.. Besides; I have to keep the function overflow and their parameters intact (since this code is a simple version of a complex class).. Anyway, I came up with a solution with the help of you all.. You can find it here: http://stackoverflow.com/questions/972460/as3-arguments It may not seem a very elusive / different approach but it's just the thing I needed.
radgar
+2  A: 

Hi, Don't know if this is still relevant but you could try this:

function aa(...aArgs):void {
    var myArray:Array = aArgs;
    bb.apply( this, myArray );
}
function bb(...bArgs):void {
    trace("bb:", bArgs.length);
}
aa(); //calling aa without any arguments.

Basically Function.apply is your friend here.

radekg
Thanks.. question solved here: http://stackoverflow.com/questions/972460/as3-arguments/972518#972518
radgar