how to pass a function as an argument to another function

Discussion in 'Javascript' started by laredotornado@zipmail.com, Jan 31, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. Lee Guest

    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)
    }


    --
     
    Lee, Jan 31, 2007
    #2
    1. Advertising

  3. Guest

    On Jan 31, 12:44 pm, Lee <> wrote:
    > 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, 2007
    #3
  4. Guest

    On Jan 31, 5:37 pm, ""
    <> wrote:
    > On Jan 31, 12:44 pm, Lee <> wrote:
    >
    >
    >
    > > 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, 2007
    #4
  5. Lee Guest

    said:
    >
    >On Jan 31, 12:44 pm, Lee <> wrote:
    >> 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);


    --
     
    Lee, Feb 1, 2007
    #5
  6. RobG Guest

    On Feb 1, 8:37 am, ""
    <> wrote:
    > On Jan 31, 12:44 pm, Lee <> wrote:
    >
    >
    >
    > > 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
     
    RobG, Feb 1, 2007
    #6
  7. 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
     
    Bruno Desthuilliers, Feb 1, 2007
    #7
  8. dd Guest

    On Jan 31, 6:41 pm, ""
    <> wrote:
    > 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.
     
    dd, Feb 1, 2007
    #8
  9. dd Guest

    On Feb 1, 10:55 am, "dd" <> wrote:
    > 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.
     
    dd, Feb 1, 2007
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Kial
    Replies:
    1
    Views:
    724
    Eric Enright
    Nov 15, 2004
  2. S?ren Gammelmark
    Replies:
    1
    Views:
    1,984
    Eric Sosman
    Jan 7, 2005
  3. Reckoner
    Replies:
    11
    Views:
    741
    Steven D'Aprano
    Jan 19, 2009
  4. AikidoGuy
    Replies:
    11
    Views:
    594
    Seebs
    Nov 21, 2011
  5. Shabam
    Replies:
    2
    Views:
    139
    Jasen Betts
    Oct 16, 2005
Loading...

Share This Page