How to construct a function call out of actual arguments array?

G

golubovsky

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.
 
E

Erwin Moller

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
 
R

RobG

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);
}
}

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).
 
R

RobG

Erwin said:
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 :)
 
R

RobG

RobG said:
Erwin said:
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);
}
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top