call function from within another

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

    call function from within another

    Say I have this:

    var foo = function(x, y) {
    return x + y;
    };

    var bar = function(fname, param1, param2) {
    /*
    * how do I call the function "foo"
    * and pass parameters to it?
    */
    window[fname];
    };

    var tot = bar(foo, 1, 2);

    I'm unsure about how to call "foo" from within "bar" and have parameters
    passed to "foo".

    Andrew Poulos
  • richard

    #2
    Re: call function from within another

    On Fri, 07 Nov 2008 14:24:40 +1100, Andrew Poulos
    <ap_prog@hotmai l.comwrote:
    >Say I have this:
    >
    >var foo = function(x, y) {
    return x + y;
    >};
    >
    >var bar = function(fname, param1, param2) {
    /*
    * how do I call the function "foo"
    * and pass parameters to it?
    */
    window[fname];
    >};
    >
    >var tot = bar(foo, 1, 2);
    >
    >I'm unsure about how to call "foo" from within "bar" and have parameters
    >passed to "foo".
    >
    >Andrew Poulos

    Might try function dothis(foo=x)
    no quotes.

    comp.lang.javas cript

    Comment

    • Andrew Poulos

      #3
      Re: call function from within another

      richard wrote:
      On Fri, 07 Nov 2008 14:24:40 +1100, Andrew Poulos
      <ap_prog@hotmai l.comwrote:
      >
      >Say I have this:
      >>
      >var foo = function(x, y) {
      > return x + y;
      >};
      >>
      >var bar = function(fname, param1, param2) {
      > /*
      > * how do I call the function "foo"
      > * and pass parameters to it?
      > */
      > window[fname];
      >};
      >>
      >var tot = bar(foo, 1, 2);
      >>
      >I'm unsure about how to call "foo" from within "bar" and have parameters
      >passed to "foo".
      >>
      >Andrew Poulos
      >
      >
      Might try function dothis(foo=x)
      no quotes.
      >
      Alas your reply is too abbreviated for me to make sense of it.

      Andrew Poulos

      Comment

      • RobG

        #4
        Re: call function from within another



        Andrew Poulos wrote:
        Say I have this:
        >
        var foo = function(x, y) {
        return x + y;
        };
        >
        var bar = function(fname, param1, param2) {
        /*
        * how do I call the function "foo"
        * and pass parameters to it?
        */
        window[fname];
        That won't work: fname is a reference to a function object, it doesn't
        know its "name" (there are many posts in clj by people wanting to know
        a function's name, and many replies saying you can't reliably tell and
        besides it's irrelevant).

        Used inside a square bracket property accessor, fname it will be
        resolved to a string by calling its toString method, so you are
        effectively trying to call:

        window[ foo.toString() ];

        which is effectively:

        window['function foo(x, y) {...}']

        };
        >
        var tot = bar(foo, 1, 2);
        >
        I'm unsure about how to call "foo" from within "bar" and have parameters
        passed to "foo".
        Not too difficult.

        The original call is global code, it passes a reference to foo() to
        bar(). That reference is assigned to bar’s local variable fname due
        to the ordering of the parameter list. So now you just call it with
        the other parameters as arguments:

        return fname(param1, param2);


        The return is there because I assume you want the result assigned to
        tot.

        --
        Rob

        Comment

        • Stevo

          #5
          Re: call function from within another

          Andrew Poulos wrote:
          Say I have this:
          >
          var foo = function(x, y) {
          return x + y;
          };
          >
          var bar = function(fname, param1, param2) {
          /*
          * how do I call the function "foo"
          * and pass parameters to it?
          */
          window[fname];
          };
          >
          var tot = bar(foo, 1, 2);
          >
          I'm unsure about how to call "foo" from within "bar" and have parameters
          passed to "foo".
          >
          Andrew Poulos
          fname(param1,pa ram2);

          Use fname exactly as you would use foo. Outside of the bar function,
          you'd call foo(param1,para m2). As you're passing foo into the bar
          function, do the same thing except using the fname ....
          fname(param1,pa ram2);

          Comment

          • slebetman

            #6
            Re: call function from within another

            On Nov 7, 11:24 am, Andrew Poulos <ap_p...@hotmai l.comwrote:
            Say I have this:
            >
            Almost right, try this:

            /* create an anonymous function,
            assign a reference to it to variable foo:
            */
            var foo = function(x, y) {
               return x + y;
            };

            var bar = function(fnref, param1, param2) {
            /* this function accepts a function reference
            (not function name, which is a string)
            and executes it, passing two parameters:
            */
            fnref(param1,pa ram2);
            };

            /* call function referred to by bar passing
            the function reference foo and integers
            1 and 2:
            */
            var tot = bar(foo, 1, 2);

            /* I personally think it is always a mistake to
            try to pass functions by "name string".
            Javascript properly supports lambdas so passing
            function references around is much more robust
            and is easier to read.
            */

            Comment

            • Michael Wojcik

              #7
              Re: call function from within another

              slebetman wrote:
              Javascript properly supports lambdas
              I don't want to be unnecessarily pedantic,[1] but "supports
              first-class functions" would be preferable. ("Supports lambda" would
              be OK, too, referring to the lambda operator. But that would likely be
              confusing for some readers, since ECMAScript doesn't have a "lambda"
              keyword, unlike LISP and friends.)

              Lambda is the name of the abstraction operator. It creates a function.
              In many languages it also creates a closure, which is the function
              plus its bound variables; many people use "closure" to refer to the
              function, which is not technically correct but close enough for many
              purposes.

              Calling a function or closure a "lambda" is like calling a sum an "add".

              What you are saying above is that ECMAScript lets you pass function
              references as parameters, and then apply them. That's a consequence of
              having first-class functions.

              Of course, there are languages where functions are not first-class,
              but you can still pass function references and apply them. C, for
              example, where functions cannot be created during program execution
              (no dynamic functions) but can be called by reference. For example:

              -----
              int foo(int x, int y) { return x + y; }
              int bar(int(*fnref) (int,int), p1, p2) { return fnref(p1, p2); }
              int main(void) { return bar(foo, 1, 2); }
              -----

              (Note that in C a function pointer is implicitly dereferenced by the
              function-call operator, so it's not necessary to write "(*fnref)" when
              calling it.)

              In COBOL, where functions ("programs") are again not first-class, you
              can call a function literally using a string literal, or through a
              reference, or using a string variable that contains its name:

              call "foo" using 1 2
              set fnref to entry "foo"
              call fnref using 1 2
              move "foo" to fname
              call fname using 1 2

              which makes call-through-reference and call-with-name-lookup almost
              indistinguishab le to the casual programmer.


              [1] This is a lie, of course. I love pedantry as much as the next poster.

              --
              Michael Wojcik
              Micro Focus
              Rhetoric & Writing, Michigan State University

              Comment

              • Dr J R Stockton

                #8
                Re: call function from within another

                In comp.lang.javas cript message <gf28af01omv@ne ws1.newsguy.com >, Fri, 7
                Nov 2008 12:48:15, Michael Wojcik <mwojcik@newsgu y.composted:
                >
                >[1] This is a lie, of course. I love pedantry as much as the next poster.
                But how can you tell who the next poster will be?

                --
                (c) John Stockton, near London. *@merlyn.demon. co.uk/?.?.Stockton@ph ysics.org
                Web <URL:http://www.merlyn.demo n.co.uk/- FAQish topics, acronyms, & links.
                Correct <= 4-line sig. separator as above, a line precisely "-- " (SoRFC1036)
                Do not Mail News to me. Before a reply, quote with ">" or "" (SoRFC1036)

                Comment

                • Michael Wojcik

                  #9
                  Re: call function from within another

                  Dr J R Stockton wrote:
                  In comp.lang.javas cript message <gf28af01omv@ne ws1.newsguy.com >, Fri, 7
                  Nov 2008 12:48:15, Michael Wojcik <mwojcik@newsgu y.composted:
                  >[1] This is a lie, of course. I love pedantry as much as the next poster.
                  >
                  But how can you tell who the next poster will be?
                  I have my newsreader sort postings by increasing pedantry.

                  --
                  Michael Wojcik
                  Micro Focus
                  Rhetoric & Writing, Michigan State University

                  Comment

                  • John G Harris

                    #10
                    Re: call function from within another

                    On Fri, 7 Nov 2008 at 12:48:15, in comp.lang.javas cript, Michael Wojcik
                    wrote:

                    <snip>
                    >Of course, there are languages where functions are not first-class,
                    >but you can still pass function references and apply them. C, for
                    >example, where functions cannot be created during program execution
                    >(no dynamic functions) but can be called by reference. For example:
                    <snip>

                    On the other hand, C++ function objects are user-defined objects that
                    can inherit, can have a type template, can have state data, and can be
                    called. The objects can be copied and different instances can have
                    different states.

                    It's a lot cleaner than using a javascript closure to do some of this.

                    John
                    --
                    John Harris

                    Comment

                    Working...