tags:

views:

24

answers:

3

Hi,

I've tried the following with no success:

function a(args){
    b(arguments);
}

function b(args){
    // arguments are lost?
}

a(1,2,3);

In function a, I can use the arguments keyword to access an array of arguments, in function b these are lost. Is there a way of passing arguments to another javascript function like I try to do?

+3  A: 

Use .apply() to have the same access to arguments in function b, like this:

function a(args){
    b.apply(this, arguments);
}
function b(args){
   alert(arguments); //arguments[0] = 1, etc
}
a(1,2,3);​

You can test it out here.

Nick Craver
+1 neat stuff '
Nikita Rybak
A: 
function a(args){
    b(args); //pass args to b
}

function b(args){
    // arguments are lost?
}

a(1,2,3);
Jamie Carruthers
A: 

this might be right...

function a(args){
    b(arguments);
}

function b(args){
    args[0] // = 1
    args[1] // = 2
    args[2] // = 3

   // arguments[0] = args so
   arguments[0][0] // = 1
   arguments[0][1] // = 2
   arguments[0][2] // = 3
}

a(1,2,3);
John Boker