HighDots Forums  

invoking a function with variable number of arguments

Javascript JavaScript language (comp.lang.javascript)


Discuss invoking a function with variable number of arguments in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
laredotornado
 
Posts: n/a

Default invoking a function with variable number of arguments - 06-16-2008 , 05:55 PM






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

Reply With Quote
  #2  
Old   
VK
 
Posts: n/a

Default Re: invoking a function with variable number of arguments - 06-16-2008 , 06:05 PM






On Jun 17, 1:55 am, laredotornado <laredotorn... (AT) zipmail (DOT) com> wrote:
Quote:
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);


Reply With Quote
  #3  
Old   
RobG
 
Posts: n/a

Default Re: invoking a function with variable number of arguments - 06-16-2008 , 06:51 PM



On Jun 17, 7:55 am, laredotornado <laredotorn... (AT) zipmail (DOT) com> wrote:
Quote:
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...nct ion:apply
Quote:


}

--
Rob


Reply With Quote
  #4  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: invoking a function with variable number of arguments - 06-16-2008 , 08:14 PM



RobG wrote:
Quote:
On Jun 17, 7:55 am, laredotornado <laredotorn... (AT) zipmail (DOT) com> wrote:
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


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.