468,771 Members | 1,482 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,771 developers. It's quick & easy.

how to pass a function as an argument to another function

Hi,

I want to pass my function

myFunc('a', 'b', 'c')

as an argument to another function. However, this will not work

doStuff('x', 'y', myFunc('a', 'b', 'c'))

because the expression "myFunc('a', 'b', 'c')" gets evalauted before
it is passed to the function "doStuff". Does this make sense?

Thanks for any help , - Dave

Jan 31 '07 #1
8 2036
Lee
la***********@zipmail.com said:
>
Hi,

I want to pass my function

myFunc('a', 'b', 'c')

as an argument to another function. However, this will not work

doStuff('x', 'y', myFunc('a', 'b', 'c'))

because the expression "myFunc('a', 'b', 'c')" gets evalauted before
it is passed to the function "doStuff". Does this make sense?

Thanks for any help , - Dave
pass the function and the arguments, separately:

dostuff('x','y',myFunc,'a','b','c');

function dostuff(a1,a2,f,b1,b2,b3) {
// do something with f(b1,b2,b3)
}
--

Jan 31 '07 #2
On Jan 31, 12:44 pm, Lee <REM0VElbspamt...@cox.netwrote:
laredotorn...@zipmail.com said:
Hi,
I want to pass my function
myFunc('a', 'b', 'c')
as an argument to another function. However, this will not work
doStuff('x', 'y', myFunc('a', 'b', 'c'))
because the expression "myFunc('a', 'b', 'c')" gets evalauted before
it is passed to the function "doStuff". Does this make sense?
Thanks for any help , - Dave

pass the function and the arguments, separately:

dostuff('x','y',myFunc,'a','b','c');

function dostuff(a1,a2,f,b1,b2,b3) {
// do something with f(b1,b2,b3)

}

--
Thanks for this creative solution. Unfortunately, I should've
mentioned that "doStuff" is not a function I defined and I cannot
change its argument list. It will only take a fixed number of
arguments, the last being a function. Is there anything else I can
do?

Thanks, - Dave

Jan 31 '07 #3
On Jan 31, 5:37 pm, "laredotorn...@zipmail.com"
<laredotorn...@zipmail.comwrote:
On Jan 31, 12:44 pm, Lee <REM0VElbspamt...@cox.netwrote:
laredotorn...@zipmail.com said:
>Hi,
>I want to pass my function
>myFunc('a', 'b', 'c')
>as an argument to another function. However, this will not work
>doStuff('x', 'y', myFunc('a', 'b', 'c'))
>because the expression "myFunc('a', 'b', 'c')" gets evalauted before
>it is passed to the function "doStuff". Does this make sense?
>Thanks for any help , - Dave
pass the function and the arguments, separately:
dostuff('x','y',myFunc,'a','b','c');
function dostuff(a1,a2,f,b1,b2,b3) {
// do something with f(b1,b2,b3)
}
--

Thanks for this creative solution. Unfortunately, I should've
mentioned that "doStuff" is not a function I defined and I cannot
change its argument list. It will only take a fixed number of
arguments, the last being a function. Is there anything else I can
do?

Thanks, - Dave
Have you tried passing in an object that has myFunc as one of it's
methods?

Feb 1 '07 #4
Lee
la***********@zipmail.com said:
>
On Jan 31, 12:44 pm, Lee <REM0VElbspamt...@cox.netwrote:
>laredotorn...@zipmail.com said:
>Hi,
>I want to pass my function
>myFunc('a', 'b', 'c')
>as an argument to another function. However, this will not work
>doStuff('x', 'y', myFunc('a', 'b', 'c'))
>because the expression "myFunc('a', 'b', 'c')" gets evalauted before
it is passed to the function "doStuff". Does this make sense?
>Thanks for any help , - Dave

pass the function and the arguments, separately:

dostuff('x','y',myFunc,'a','b','c');

function dostuff(a1,a2,f,b1,b2,b3) {
// do something with f(b1,b2,b3)

}

--

Thanks for this creative solution. Unfortunately, I should've
mentioned that "doStuff" is not a function I defined and I cannot
change its argument list. It will only take a fixed number of
arguments, the last being a function. Is there anything else I can
do?
If it's expecting a function, it's not expecting that function be provided with
arguments. Your other option is to create a wrapper function to be passed to
doStuff(), and have that wrapper function call myFunc() with your arguments:

function wrapper() {
return myFunc('a','b','c');
}

doStuff('x','y',wrapper);
--

Feb 1 '07 #5
On Feb 1, 8:37 am, "laredotorn...@zipmail.com"
<laredotorn...@zipmail.comwrote:
On Jan 31, 12:44 pm, Lee <REM0VElbspamt...@cox.netwrote:
laredotorn...@zipmail.com said:
>Hi,
>I want to pass my function
>myFunc('a', 'b', 'c')
>as an argument to another function. However, this will not work
>doStuff('x', 'y', myFunc('a', 'b', 'c'))
>because the expression "myFunc('a', 'b', 'c')" gets evalauted before
>it is passed to the function "doStuff". Does this make sense?
>Thanks for any help , - Dave
pass the function and the arguments, separately:
dostuff('x','y',myFunc,'a','b','c');
function dostuff(a1,a2,f,b1,b2,b3) {
// do something with f(b1,b2,b3)
}
--

Thanks for this creative solution. Unfortunately, I should've
mentioned that "doStuff" is not a function I defined and I cannot
change its argument list. It will only take a fixed number of
arguments, the last being a function. Is there anything else I can
do?
Yes - pass a function object like:

soStuff( a, b, c, function(){myFunc(arg1, arg2, arg3);} )

e.g.

function myFunc (arg1, arg2){
alert('myFunc called with ' + arg1 + ' and ' + arg2);
}

function doStuff (arg1, arg2, fn) {
alert('About to call myFunc from doStuff');
fn();
}

function bar(){
doStuff ('one', 'two', function(){myFunc ('bar1','bar2');});
}

bar();

--
Rob
Feb 1 '07 #6
la***********@zipmail.com a écrit :
Hi,

I want to pass my function

myFunc('a', 'b', 'c')

as an argument to another function. However, this will not work

doStuff('x', 'y', myFunc('a', 'b', 'c'))

because the expression "myFunc('a', 'b', 'c')" gets evalauted before
it is passed to the function "doStuff". Does this make sense?
Q&D, not tested, but this should get you started:

partial = function(f, a, b, c) {
return function() {
return f(a, b, c);
};
}

doStuff('x', 'y', partial(myFunc, 'a', 'b', 'c'));

HTH
Feb 1 '07 #7
dd
On Jan 31, 6:41 pm, "laredotorn...@zipmail.com"
<laredotorn...@zipmail.comwrote:
I want to pass my function
myFunc('a', 'b', 'c')
as an argument to another function. However, this will not work
doStuff('x', 'y', myFunc('a', 'b', 'c'))
I'm sure I'll get shouted at for using it, but here's how I
solved that problem:

doStuff('x', 'y', "myFunc('a', 'b', 'c')" )

When the time came, inside doStuff, to make the
call, I just eval'ed the 3rd param:

function doStuff(p1, p2, p3) {
bla,bla,bla
eval(p3);
}

If 'a', 'b' and 'c' are not fixed values but are variables
instead, then breaking open the double-quoted string
let's you get at the values:

"myFunc("+a+","+b+","+c+")"

If any of them are strings, then you need extra quotes
to ensure the eval doesn't think they're variables, eg:

"myFunc('"+a+"','"+b+"','"+c+"')"

I think people give eval too much bad press. It can get
you around some difficult problems like this one. Sure
if a proper solution comes up then switch to that, but
as long as you don't have one, this can keep your code
at least doing what it should.

Feb 1 '07 #8
dd
On Feb 1, 10:55 am, "dd" <dd4...@gmail.comwrote:
When the time came, inside doStuff, to make the
call, I just eval'ed the 3rd param:
Of course if myFunc was meant to form a closure then
this is unsuitable. I'm assuming it's a function with
global scope.

Feb 1 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Satish Chimakurthi | last post: by
4 posts views Thread by Pushkar Pradhan | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.