pass function into another function as parameter?

Discussion in 'Javascript' started by soni2926, Nov 20, 2008.

  1. soni2926

    soni2926 Guest

    hi,
    is it possible to pass a function into another function as a
    parameter? Say i have these:

    function SaveMe(text)
    {...}

    function SaveMeNow(text)
    {...}

    function WhichToSave(x, y, z)
    {...}

    a button will call WhichToSave, that function will perform some logic
    and then call one of the Save methods, can i pass that method in as
    say parameter z and have it just call the function right away, passing
    one of the SaveMe methods a parameter which is determined by the
    WhichToSave method?

    Thanks.
    soni2926, Nov 20, 2008
    #1
    1. Advertising

  2. soni2926

    dhtml Guest

    On Nov 20, 3:57 pm, soni2926 <> wrote:
    > hi,
    > is it possible to pass a function into another function as a
    > parameter?  Say i have these:
    >
    > function SaveMe(text)
    > {...}
    >
    > function SaveMeNow(text)
    > {...}
    >
    > function WhichToSave(x, y, z)
    > {...}
    >
    > a button will call WhichToSave,


    How does a button call something? Is WhichToSave a callback from an
    event handler attached to a button, as in:-

    button.onclick = WhichToSave;

    ?

    > that function will perform some logic
    > and then call one of the Save methods, can i pass that method in as
    > say parameter z and have it just call the function right away, passing
    > one of the SaveMe methods a parameter which is determined by the
    > WhichToSave method?
    >


    Functions can be passed.

    function invoker(f) {
    f();
    };

    function a() {
    document.title = "a";
    }

    invoker(a);

    Garrett

    > Thanks.
    dhtml, Nov 21, 2008
    #2
    1. Advertising

  3. On Nov 20, 9:57 pm, soni2926 <> wrote:
    > hi,
    > is it possible to pass a function into another function as a
    > parameter?

    Yes.

    function foo(){
    alert('foo');
    }

    function bar(fn){
    fn();
    }

    bar(foo); // alerts 'foo'

    --
    Gabriel Gilini
    Gabriel Gilini, Nov 21, 2008
    #3
  4. soni2926

    soni2926 Guest

    On Nov 20, 7:13 pm, Gabriel Gilini <> wrote:
    > On Nov 20, 9:57 pm,soni2926<> wrote:> hi,
    > > is it possible to pass a function into another function as a
    > > parameter?

    >
    > Yes.
    >
    > function foo(){
    >    alert('foo');
    >
    > }
    >
    > function bar(fn){
    >    fn();
    >
    > }
    >
    > bar(foo); // alerts 'foo'
    >
    > --
    > Gabriel Gilini


    thank you!
    soni2926, Nov 21, 2008
    #4
  5. On Nov 20, 5:57 pm, soni2926 <> wrote:
    > hi,
    > is it possible to pass a function into another function as a
    > parameter?


    function reverse_str( s )
    { return s.split("").reverse().join("")
    }

    function upcase( s )
    { return s.toUpperCase()
    }

    funcs = [reverse_str, upcase]
    for (var i = 0; i<funcs.length; i++)
    document.write( "<p>" + funcs( "was i able" ) )
    William James, Nov 21, 2008
    #5
    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. deanfamily
    Replies:
    4
    Views:
    400
    puzzlecracker
    Oct 24, 2005
  2. QQ
    Replies:
    3
    Views:
    386
    Jens Thoms Toerring
    May 9, 2007
  3. grbgooglefan
    Replies:
    9
    Views:
    845
    grbgooglefan
    May 9, 2008
  4. Renie83
    Replies:
    2
    Views:
    218
  5. AzamSharp
    Replies:
    2
    Views:
    159
Loading...

Share This Page