pass function into another function as parameter?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • soni2926

    pass function into another function as parameter?

    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.
  • dhtml

    #2
    Re: pass function into another function as parameter?

    On Nov 20, 3:57 pm, soni2926 <soni2...@yahoo .comwrote:
    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.

    Comment

    • Gabriel Gilini

      #3
      Re: pass function into another function as parameter?

      On Nov 20, 9:57 pm, soni2926 <soni2...@yahoo .comwrote:
      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

      Comment

      • soni2926

        #4
        Re: pass function into another function as parameter?

        On Nov 20, 7:13 pm, Gabriel Gilini <gabr...@usosim .com.brwrote:
        On Nov 20, 9:57 pm,soni2926<son i2...@yahoo.com 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!

        Comment

        • William James

          #5
          Re: pass function into another function as parameter?

          On Nov 20, 5:57 pm, soni2926 <soni2...@yahoo .comwrote:
          hi,
          is it possible to pass a function into another function as a
          parameter?
          function reverse_str( s )
          { return s.split("").rev erse().join("")
          }

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

          funcs = [reverse_str, upcase]
          for (var i = 0; i<funcs.length ; i++)
          document.write( "<p>" + funcs[i]( "was i able" ) )

          Comment

          Working...