Pass array elements as function arguments

F

FrederikVds

I have a function which can be called with an unlimited number of
arguments.
I want to call another function with exactly the same arguments. I know
I can get the arguments in the arguments object, and as such also in an
array, but how do you pass the elements of an array to another function
as parameters?

I hope you understand what I mean.

Thanks,
Frederik Vanderstraeten
 
M

Martin Honnen

FrederikVds said:
I have a function which can be called with an unlimited number of
arguments.
I want to call another function with exactly the same arguments. I know
I can get the arguments in the arguments object, and as such also in an
array, but how do you pass the elements of an array to another function
as parameters?

Use the apply method of functions e.g.

function f () {
alert(arguments.length);
}
function g () {
f.apply(this, arguments);
}

g(1, 2, 3);

see
<http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Function:apply>
 
D

Dr J R Stockton

In comp.lang.javascript message
Tue said:
I have a function which can be called with an unlimited number of
arguments.
I want to call another function with exactly the same arguments. I know
I can get the arguments in the arguments object, and as such also in an
array, but how do you pass the elements of an array to another function
as parameters?

Rewrite the function to take a single parameter which will be an Object
holding all the previous parameters. You can easily pass that Object in
the next call. Untested.

That should work in older browsers; subtler ways may only work in newer
ones.
 
F

Frederik Vanderstraeten

Dr J R Stockton schreef:
In comp.lang.javascript message


Rewrite the function to take a single parameter which will be an Object
holding all the previous parameters. You can easily pass that Object in
the next call. Untested.

That should work in older browsers; subtler ways may only work in newer
ones.

That's not really possible as some of the functions I call may be
built-in functions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top