473,387 Members | 3,684 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
4 1969
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

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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
6
by: Richard Berg | last post by:
Hello, I need implementation advice: I am writing a program that should call some API functions in a specific order. The APIs are for another application, not Windows APIs. The order and type...
1
by: Foxy Kav | last post by:
Hi everyone, im a first year UNI student doing a programming subject and im stuck on how to get rid of my global variables, char stringarray and char emptystring. I was wondering if anyone could...
17
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int...
21
by: dragoncoder | last post by:
Consider the following code. #include <stdio.h> int main() { int i =1; printf("%d ,%d ,%d\n",i,++i,i++); return 0; }
7
by: lordkain | last post by:
Hello all, I have a problem which I cant seem to solve, I dont know if it is solvable.. it should though. What I want is to call a function, whith x arguments on base of an xml file. In the...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
5
by: Joakim Hove | last post by:
Hello, I have a function like this: /* It actually does something else .... */ void func (int size, const char ** string_list) { int i; for (i=0; i < size; i++) printf("String number %d:...
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.