P: n/a

Hi,
Is there an easy way to construct a function call out of a function and
an array of actual arguments?
e. g. given a function `fun' and an array [1,2,3]
I need to obtain equivalent of `fun(1,2,3)'
`fun' is not a method of any object, just a function. Can `apply' be
used for this purpose?
I'd like to avoid using `eval' although it might be possible to
construct a string and evaluate it.
Thanks for any ideas.  
Share this Question
P: n/a
 go********@gmail.com wrote:
Hi,
Is there an easy way to construct a function call out of a function and
an array of actual arguments?
e. g. given a function `fun' and an array [1,2,3]
I need to obtain equivalent of `fun(1,2,3)'
`fun' is not a method of any object, just a function. Can `apply' be
used for this purpose?
I'd like to avoid using `eval' although it might be possible to
construct a string and evaluate it.
Thanks for any ideas.
Hi,
This may be very obvious, but why not pass the array to fun and let fun pick
the values out?
Like this:
var funArr = new Array(1,2,3);
fun(funArr);
function fun(theArr){
var arg1 = theArr[0];
var arg2 = theArr[1];
var arg3 = theArr[2];
// go on here with arg1, arg2, arg3
}
Or will that not work in your situation?
Regards,
Erwin Moller  
P: n/a
 go********@gmail.com wrote:
Hi,
Is there an easy way to construct a function call out of a function and
an array of actual arguments?
e. g. given a function `fun' and an array [1,2,3]
I need to obtain equivalent of `fun(1,2,3)'
`fun' is not a method of any object, just a function. Can `apply' be
used for this purpose?
Yes, as long as you know what you want to set for the this operator:
function x(){
for (var i=0, len=arguments.length; i<len; i++){
alert(arguments[i]);
}
}
var y = [1, 2, 3];
x.apply(this, y);
Sets this to the global/window object and alerts 1, then 2, then 3.
Note that the apply method is not supported in IE 5.0 (and probably
some other early browsers too).

Rob  
P: n/a

Erwin Moller wrote:
go********@gmail.com wrote:
Hi,
Is there an easy way to construct a function call out of a function and
an array of actual arguments?
e. g. given a function `fun' and an array [1,2,3]
I need to obtain equivalent of `fun(1,2,3)'
`fun' is not a method of any object, just a function. Can `apply' be
used for this purpose?
I'd like to avoid using `eval' although it might be possible to
construct a string and evaluate it.
Thanks for any ideas.
Hi,
This may be very obvious, but why not pass the array to fun and let fun pick
the values out?
Possibly because the call method allows you to use the arguments object
of one function as the parameters in the call to another function:
function x(){
y.call(this, arguments);
}
function y(){
/* arguments is whatever was passed to x() */
}
x(arg1, arg2, ... argN);
But that is just a guess :)

Rob  
P: n/a

RobG wrote:
Erwin Moller wrote:
go********@gmail.com wrote:
Hi,
>
Is there an easy way to construct a function call out of a function and
an array of actual arguments?
>
e. g. given a function `fun' and an array [1,2,3]
>
I need to obtain equivalent of `fun(1,2,3)'
>
`fun' is not a method of any object, just a function. Can `apply' be
used for this purpose?
>
I'd like to avoid using `eval' although it might be possible to
construct a string and evaluate it.
>
Thanks for any ideas.
Hi,
This may be very obvious, but why not pass the array to fun and let fun pick
the values out?
Possibly because the call method ...
Agghhh, what was I on? I meant apply of course.
function x(){
y.apply(this, arguments);
}

Rob   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 1653
 replies: 4
 date asked: Sep 14 '06
