By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,384 Members | 1,886 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,384 IT Pros & Developers. It's quick & easy.

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

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.

Sep 14 '06 #1
Share this Question
Share on Google+
4 Replies


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
Sep 14 '06 #2

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

Sep 14 '06 #3

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

Sep 14 '06 #4

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

Sep 14 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.