473,473 Members | 1,604 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

invoking a function with variable number of arguments

Hi,

I'm passing a function, "myFunc" as an argument to another function,
"doFunc". In "doFunc", I am creating an arbitrary number of arguments
to pass to "myFunc". How do I invoke "myFunc" with an arguments array
stored in "argsArr"? Here is the basic function skeleton:

function doFunc(myFunc) {
var argsArr = getArgsArray();
// invoke myFunc with argsArr ... how?
}

Thanks for the help, - Dave
Jun 27 '08 #1
3 1235
VK
On Jun 17, 1:55 am, laredotornado <laredotorn...@zipmail.comwrote:
Hi,

I'm passing a function, "myFunc" as an argument to another function,
"doFunc". In "doFunc", I am creating an arbitrary number of arguments
to pass to "myFunc". How do I invoke "myFunc" with an arguments array
stored in "argsArr"? Here is the basic function skeleton:

function doFunc(myFunc) {
var argsArr = getArgsArray();
// invoke myFunc with argsArr ... how?

}
function doFunc(myFunc) {
myFunc.apply(null,getArgsArray());
}

function getArgsArray() {
return [1,2,3];
}

function f() {
for (var i=0; i<arguments.length; i++) {
window.alert(arguments[i]);
}
}

doFunc(f);
Jun 27 '08 #2
On Jun 17, 7:55 am, laredotornado <laredotorn...@zipmail.comwrote:
Hi,

I'm passing a function, "myFunc" as an argument to another function,
"doFunc". In "doFunc", I am creating an arbitrary number of arguments
to pass to "myFunc". How do I invoke "myFunc" with an arguments array
stored in "argsArr"? Here is the basic function skeleton:

function doFunc(myFunc) {
var argsArr = getArgsArray();
// invoke myFunc with argsArr ... how?
Use Function.prototype.apply:

myFunc.apply(window, argsArr);
<URL: http://developer.mozilla.org/en/docs...Function:apply
>
>
}

--
Rob
Jun 27 '08 #3
RobG wrote:
On Jun 17, 7:55 am, laredotornado <laredotorn...@zipmail.comwrote:
>I'm passing a function, "myFunc" as an argument to another function,
"doFunc". In "doFunc", I am creating an arbitrary number of arguments
to pass to "myFunc". How do I invoke "myFunc" with an arguments array
stored in "argsArr"? Here is the basic function skeleton:

function doFunc(myFunc) {
var argsArr = getArgsArray();
// invoke myFunc with argsArr ... how?

Use Function.prototype.apply:

myFunc.apply(window, argsArr);
Needlessly proprietary and error-prone. Should be

myFunc.apply(this, argsArr);

in global context and

myFunc.apply(global, argsArr);

in local context with `global' assigned as

var global = this;

in global context before.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
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) {...
7
by: Kapt. Boogschutter | last post by:
I'm trying to create a function that has at least 1 Argument but can also contain any number of Arguments (except 0 because my function would have no meaning for 0 argument). The arguments...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
10
by: Robert Skidmore | last post by:
Take a look at this new JS function I made. It is really simple but very powerful. You can animate any stylesheet numeric value (top left width height have been tested), and works for both % and px...
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
3
by: carvalho.miguel | last post by:
hello, imagine you have a static class method that receives a function pointer, an int with the number of arguments and a variable number of arguments. in that static method you want to call...
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) {
9
by: CryptiqueGuy | last post by:
Consider the variadic function with the following prototype: int foo(int num,...); Here 'num' specifies the number of arguments, and assume that all the arguments that should be passed to this...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.