how to pass a function as an argument to another function

L

laredotornado

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
 
L

Lee

(e-mail address removed) 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)
}


--
 
L

laredotornado

(e-mail address removed) said:










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
 
C

Curtis.DanielN

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?
 
L

Lee

(e-mail address removed) said:
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);


--
 
R

RobG

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();
 
B

Bruno Desthuilliers

(e-mail address removed) 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
 
D

dd

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.
 
D

dd

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top