calling function with "return function()"

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

    calling function with "return function()"

    On page 392 of "Javascript the definitive guide" a function is called
    like this:-

    <form action="process form.cgi" onsubmit="retur n validateForm(); ">

    Why, in this instance, is the return statement used in calling the
    function validateForm rather than being included inside the function?

    Thanks in advance,
  • Joost Diepenmaat

    #2
    Re: calling function with &quot;return function()&quot ;

    Steve <stephen.joung@ googlemail.comw rites:
    On page 392 of "Javascript the definitive guide" a function is called
    like this:-
    >
    <form action="process form.cgi" onsubmit="retur n validateForm(); ">
    >
    Why, in this instance, is the return statement used in calling the
    function validateForm rather than being included inside the function?
    Because onsubmit="retur n validateForm(); " is roughly equivalent to

    element.onsubmi t = function() { return validateForm(); };

    While, onsubmit="valid ateForm();" would be equivalent to

    element.onsubmi t = function() { validateForm(); };

    Also note that there is direct equivalent to element.submit = validateForm;


    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

    Comment

    • Joost Diepenmaat

      #3
      Re: calling function with &quot;return function()&quot ;

      Joost Diepenmaat <joost@zeekat.n lwrites:
      Also note that there is direct equivalent to element.submit = validateForm;
      ^^^^^^^^^ I meant "...is NO direct..."

      --
      Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: calling function with &quot;return function()&quot ;

        Joost Diepenmaat wrote:
        Joost Diepenmaat <joost@zeekat.n lwrites:
        >Also note that there is direct equivalent to element.submit = validateForm;
        ^^^^^^^^^ I meant "...is NO direct..."
        There is, but it makes no sense in the first place. If it worked, you would
        be overwriting the form object's submit() method.


        PointedEars
        --
        Prototype.js was written by people who don't know javascript for people
        who don't know javascript. People who don't know javascript are not
        the best source of advice on designing systems that use javascript.
        -- Richard Cornford, cljs, <f806at$ail$1$8 300dec7@news.de mon.co.uk>

        Comment

        • Steve

          #5
          Re: calling function with &quot;return function()&quot ;

          On Jun 7, 8:11 pm, Joost Diepenmaat <jo...@zeekat.n lwrote:
          Steve <stephen.jo...@ googlemail.comw rites:
          On page 392 of "Javascript the definitive guide" a function is called
          like this:-
          >
          <form action="process form.cgi" onsubmit="retur n validateForm(); ">
          >
          Why, in this instance, is the return statement used in calling the
          function validateForm rather than being included inside the function?
          >
          Because onsubmit="retur n validateForm(); " is roughly equivalent to
          >
          element.onsubmi t = function() { return validateForm(); };
          >
          While, onsubmit="valid ateForm();" would be equivalent to
          >
          element.onsubmi t = function() { validateForm(); };
          >
          Thanks for the answer but I still don't get it. What would be the
          difference in effect between calling

          "element.onsubm it = function() { return validateForm(); };" and
          calling

          "element.onsubm it = function() { validateForm(); };" ?

          Cheers.

          Comment

          • Joost Diepenmaat

            #6
            Re: calling function with &quot;return function()&quot ;

            Steve <stephen.joung@ googlemail.comw rites:
            Thanks for the answer but I still don't get it. What would be the
            difference in effect between calling
            >
            "element.onsubm it = function() { return validateForm(); };" and
            calling
            >
            "element.onsubm it = function() { validateForm(); };" ?
            the second variant doesn't propagate the return value of
            validateForm() to the form. you usually want to, because returning
            false from an onsubmit handler will cancel the submit (while returning
            nothing at all or true would cause the submit to continue). Not much
            point in checking form fields and then just submitting it if it
            doesn't validate.

            --
            Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

            Comment

            • Steve

              #7
              Re: calling function with &quot;return function()&quot ;

              On Jun 7, 9:22 pm, Joost Diepenmaat <jo...@zeekat.n lwrote:
              Steve <stephen.jo...@ googlemail.comw rites:
              Thanks for the answer but I still don't get it. What would be the
              difference in effect between calling
              >
              "element.onsubm it = function() { return validateForm(); };" and
              calling
              >
              "element.onsubm it = function() { validateForm(); };" ?
              >
              the second variant doesn't propagate the return value of
              validateForm() to the form. you usually want to, because returning
              false from an onsubmit handler will cancel the submit (while returning
              nothing at all or true would cause the submit to continue). Not much
              point in checking form fields and then just submitting it if it
              doesn't validate.
              >
              --
              Joost Diepenmaat | blog:http://joost.zeekat.nl/| work:http://zeekat.nl/
              Yes but why not include the return statement inside the function, like
              this:

              function validateForm() {
              if (document.forms[1].element[2]==null) {
              return false;
              }
              else {
              return true;
              }
              }

              Cheers

              Comment

              • Joost Diepenmaat

                #8
                Re: calling function with &quot;return function()&quot ;

                Steve <stephen.joung@ googlemail.comw rites:
                On Jun 7, 9:22 pm, Joost Diepenmaat <jo...@zeekat.n lwrote:
                >Steve <stephen.jo...@ googlemail.comw rites:
                Thanks for the answer but I still don't get it. What would be the
                difference in effect between calling
                >>
                "element.onsubm it = function() { return validateForm(); };" and
                calling
                >>
                "element.onsubm it = function() { validateForm(); };" ?
                >>
                >the second variant doesn't propagate the return value of
                >validateForm () to the form. you usually want to, because returning
                >false from an onsubmit handler will cancel the submit (while returning
                >nothing at all or true would cause the submit to continue). Not much
                >point in checking form fields and then just submitting it if it
                >doesn't validate.
                >>
                Yes but why not include the return statement inside the function, like
                this:
                >
                function validateForm() {
                if (document.forms[1].element[2]==null) {
                return false;
                }
                else {
                return true;
                }
                }
                >
                What I'm saying is you need the return statement(s) in the
                validateForm function AND in the anonymous function.

                --
                Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

                Comment

                • Tom de Neef

                  #9
                  Re: calling function with &quot;return function()&quot ;

                  "Steve" <stephen.joung@ googlemail.coms chreef in bericht
                  news:af923168-b93c-47c5-a8ed-19c8aa813ec8@s5 0g2000hsb.googl egroups.com...
                  On Jun 7, 9:22 pm, Joost Diepenmaat <jo...@zeekat.n lwrote:
                  >Steve <stephen.jo...@ googlemail.comw rites:
                  Thanks for the answer but I still don't get it. What would be the
                  difference in effect between calling
                  >>
                  "element.onsubm it = function() { return validateForm(); };" and
                  calling
                  >>
                  "element.onsubm it = function() { validateForm(); };" ?
                  >>
                  >the second variant doesn't propagate the return value of
                  >validateForm () to the form. you usually want to, because returning
                  >false from an onsubmit handler will cancel the submit (while returning
                  >nothing at all or true would cause the submit to continue). Not much
                  >point in checking form fields and then just submitting it if it
                  >doesn't validate.
                  >>
                  >--
                  >Joost Diepenmaat | blog:http://joost.zeekat.nl/| work:http://zeekat.nl/
                  >
                  Yes but why not include the return statement inside the function, like
                  this:
                  >
                  function validateForm() {
                  if (document.forms[1].element[2]==null) {
                  return false;
                  }
                  else {
                  return true;
                  }
                  }
                  >
                  Gentlemen experts, would it be correct to explain it in this way? :

                  Consider what you would have to code in the case there is no function but a
                  predefined value. For instance, you want to disable the submission of form
                  input.
                  You could have:
                  var submissionBlock = false;
                  element.onsubmi t = return submissionBlock ;
                  Meaning: pass the value of submissionBlock to the form. If you had
                  element.onsubmi t = submissionBlock
                  it would assign the value of submissionBlock to the property onsubmit of
                  element (which would not affect the form's excution).

                  Tom


                  Comment

                  • VK

                    #10
                    Re: calling function with &quot;return function()&quot ;

                    On Jun 7, 11:47 pm, Steve <stephen.jo...@ googlemail.comw rote:
                    On Jun 7, 9:22 pm, Joost Diepenmaat <jo...@zeekat.n lwrote:
                    >
                    >
                    >
                    Steve <stephen.jo...@ googlemail.comw rites:
                    Thanks for the answer but I still don't get it. What would be the
                    difference in effect between calling
                    >
                    "element.onsubm it = function() { return validateForm(); };" and
                    calling
                    >
                    "element.onsubm it = function() { validateForm(); };" ?
                    >
                    the second variant doesn't propagate the return value of
                    validateForm() to the form. you usually want to, because returning
                    false from an onsubmit handler will cancel the submit (while returning
                    nothing at all or true would cause the submit to continue). Not much
                    point in checking form fields and then just submitting it if it
                    doesn't validate.
                    >
                    --
                    Joost Diepenmaat | blog:http://joost.zeekat.nl/|work:http://zeekat.nl/
                    >
                    Yes but why not include the return statement inside the function, like
                    this:
                    >
                    function validateForm() {
                    if (document.forms[1].element[2]==null) {
                    return false;
                    }
                    else {
                    return true;
                    }
                    >
                    }
                    You could check it yourself: in this case the form will be submitted
                    in either case _if_ validateForm is called from the intrinsic event
                    handler.

                    <form ... onsubmit="valid ateForm(this)">
                    is in fact internally
                    <form ... onsubmit= function anonymous() { validateForm(th is); }>

                    It means that no matter what validateForm returns, function anonymous
                    returns nothing (undefined) which is treated as a permission to submit
                    the form. It is an initial DOM 0 algorithm "oops" which is now
                    sustained by an ocean of existing scripts, so it is very hard (~=
                    impossible) to change it. Someone was thinking Perl I guess while
                    defining the procedure so he/she forgot that Javascript doesn't have
                    implicit return values for subroutines. The right algorithm would be
                    "if intrinsic onsubmit handler is set but returns nothing then treat
                    it as if it returns false". Too late to cry over it anyway. So the
                    regular algorithm fix is by using "piped return" like
                    <form ... onsubmit="retur n validateForm(th is)">

                    Comment

                    • Thomas 'PointedEars' Lahn

                      #11
                      Re: calling function with &quot;return function()&quot ;

                      Tom de Neef wrote:
                      "Steve" <stephen.joung@ googlemail.coms chreef [...]:
                      >On Jun 7, 9:22 pm, Joost Diepenmaat <jo...@zeekat.n lwrote:
                      >>Steve <stephen.jo...@ googlemail.comw rites:
                      >>>Thanks for the answer but I still don't get it. What would be the
                      >>>difference in effect between calling
                      >>>"element.ons ubmit = function() { return validateForm(); };" and
                      >>>calling
                      >>>"element.ons ubmit = function() { validateForm(); };" ?
                      >>the second variant doesn't propagate the return value of
                      >>validateForm( ) to the form. you usually want to, because returning
                      >>false from an onsubmit handler will cancel the submit (while returning
                      >>nothing at all or true would cause the submit to continue). Not much
                      >>point in checking form fields and then just submitting it if it
                      >>doesn't validate.
                      >Yes but why not include the return statement inside the function, like
                      >this:
                      >>
                      >function validateForm() {
                      > if (document.forms[1].element[2]==null) {
                      > return false;
                      > }
                      > else {
                      > return true;
                      > }
                      >}
                      >
                      Gentlemen experts, would it be correct to explain it in this way? :
                      Of course not.
                      Consider what you would have to code in the case there is no function but a
                      predefined value. For instance, you want to disable the submission of form
                      input.
                      You could have:
                      var submissionBlock = false;
                      element.onsubmi t = return submissionBlock ;
                      >
                      Meaning: pass the value of submissionBlock to the form.
                      This is a syntax error instead: `return' has been used outside of a function.
                      If you had
                      element.onsubmi t = submissionBlock
                      it would assign the value of submissionBlock to the property onsubmit of
                      element (which would not affect the form's excution).
                      Certainly not.


                      PointedEars
                      --
                      realism: HTML 4.01 Strict
                      evangelism: XHTML 1.0 Strict
                      madness: XHTML 1.1 as application/xhtml+xml
                      -- Bjoern Hoehrmann

                      Comment

                      • Steve

                        #12
                        Re: calling function with &quot;return function()&quot ;

                        On Jun 7, 9:59 pm, Joost Diepenmaat <jo...@zeekat.n lwrote:
                        >
                        What I'm saying is you need the return statement(s) in the
                        validateForm function AND in the anonymous function.
                        >
                        And what I am asking is why.

                        Cheers, Steve.

                        Comment

                        • Jorge

                          #13
                          Re: calling function with &quot;return function()&quot ;

                          On Jun 9, 11:16 am, Steve <stephen.jo...@ googlemail.comw rote:
                          On Jun 7, 9:59 pm, Joost Diepenmaat <jo...@zeekat.n lwrote:
                          >
                          What I'm saying is you need the return statement(s) in the
                          validateForm function AND in the anonymous function.
                          >
                          And what I am asking is why.
                          >
                          Because (afaik... ) :

                          1.- element.onsubmi t= validateForm; --returns whatever
                          validateForm() returns.

                          2.- element.onsubmi t= "validateForm() "; is element.onsubmi t= function
                          () { validateForm() }; in disguise.

                          3.- element.onsubmi t= "return validateForm()" ; is element.onsubmi t=
                          function () { return validateForm() }; in disguise.

                          1 and 3 will return whatever validateForm() returns, but 2 will always
                          return undefined regardless :

                          <html><head><sc ript>
                          window.validate Form= function (e) { return "returned value" };
                          window.$= function (p) { return document.getEle mentById(p) };
                          window.onload= function () {

                          $('id1').onclic k= validateForm;
                          alert("1: "+($('id1')).on click);
                          alert("2: "+($('id2')).on click);
                          alert("3: "+($('id3')).on click);

                          };
                          </script></head><body>
                          <button id="id1">1</button>
                          <button id="id2" onclick= "validateForm() ">2</button>
                          <button id="id3" onclick= "return validateForm()" >3</button>
                          </body></html>

                          Not that this answers your question, though...

                          --Jorge.

                          Comment

                          • Joost Diepenmaat

                            #14
                            Re: calling function with &quot;return function()&quot ;

                            Steve <stephen.joung@ googlemail.comw rites:
                            On Jun 7, 9:59 pm, Joost Diepenmaat <jo...@zeekat.n lwrote:
                            >>
                            >What I'm saying is you need the return statement(s) in the
                            >validateForm function AND in the anonymous function.
                            >>
                            And what I am asking is why.
                            Because without a return statement there will be no returned
                            data. There really isn't much to explain; javascript REQUIRES a return
                            statement to return data.

                            Example:

                            function a() {
                            return "a";
                            }

                            function b() {
                            return a();
                            }

                            function c() {
                            a();
                            }


                            calls evaluating to

                            a() ="a"
                            b() ="a"
                            c() =undefined

                            --
                            Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

                            Comment

                            Working...