Calling a function when passing no parameters

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

    Calling a function when passing no parameters

    If I have the following function:

    function foo(){

    alert('hi');

    }

    and I don't need to pass any parameters to it, is calling it this way:

    foo;

    the same as calling it this way:

    foo();
  • Joost Diepenmaat

    #2
    Re: Calling a function when passing no parameters

    Yansky <thegooddale@gm ail.comwrites:
    If I have the following function:
    >
    function foo(){
    >
    alert('hi');
    >
    }
    >
    and I don't need to pass any parameters to it, is calling it this way:
    >
    foo;
    >
    the same as calling it this way:
    >
    foo();
    No. foo; does not call it at all. It's a statement returning the
    function foo, but since you're not doing anything with the value it's
    a useless statement.

    A contrived example of foo; is:

    var bar = foo; // assign function foo to bar
    bar(); // call the function.

    IOW, you do need the parentheses if you want to call a function.



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

    Comment

    • Yansky

      #3
      Re: Calling a function when passing no parameters

      On Jul 18, 8:12 am, Joost Diepenmaat <jo...@zeekat.n lwrote:
      Yansky <thegoodd...@gm ail.comwrites:
      If I have the following function:
      >
      function foo(){
      >
        alert('hi');
      >
      }
      >
      and I don't need to pass any parameters to it, is calling it this way:
      >
      foo;
      >
      the same as calling it this way:
      >
      foo();
      >
      No. foo; does not call it at all. It's a statement returning the
      function foo, but since you're not doing anything with the value it's
      a useless statement.
      >
      A contrived example of foo; is:
      >
      var bar = foo; // assign function foo to bar
      bar();  // call the function.
      >
      IOW, you do need the parentheses if you want to call a function.
      >
      --
      Joost Diepenmaat | blog:http://joost.zeekat.nl/| work:http://zeekat.nl/
      Thanks for clearing that up. :)

      Comment

      • Yansky

        #4
        Re: Calling a function when passing no parameters

        On Jul 18, 8:40 am, Yansky <thegoodd...@gm ail.comwrote:
        On Jul 18, 8:12 am, Joost Diepenmaat <jo...@zeekat.n lwrote:
        >
        >
        >
        Yansky <thegoodd...@gm ail.comwrites:
        If I have the following function:
        >
        function foo(){
        >
          alert('hi');
        >
        }
        >
        and I don't need to pass any parameters to it, is calling it this way:
        >
        foo;
        >
        the same as calling it this way:
        >
        foo();
        >
        No. foo; does not call it at all. It's a statement returning the
        function foo, but since you're not doing anything with the value it's
        a useless statement.
        >
        A contrived example of foo; is:
        >
        var bar = foo; // assign function foo to bar
        bar();  // call the function.
        >
        IOW, you do need the parentheses if you want to call a function.
        >
        --
        Joost Diepenmaat | blog:http://joost.zeekat.nl/|work:http://zeekat.nl/
        >
        Thanks for clearing that up. :)
        Oh wait I have one more question. What about when you're calling the
        function inside an event listener? e.g.
        function foo(){

        alert('hi');

        }
        window.addEvent Listener("load" , foo, false);

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: Calling a function when passing no parameters

          Yansky wrote:
          On Jul 18, 8:12 am, Joost Diepenmaat <jo...@zeekat.n lwrote:
          >Yansky <thegoodd...@gm ail.comwrites:
          >>If I have the following function:
          >>function foo(){
          >> alert('hi');
          >>}
          >>and I don't need to pass any parameters to it, is calling it this way:
          >>foo;
          >>the same as calling it this way:
          >>foo();
          That's probably the second most stupid question I have ever read on Usenet.
          >No. foo; does not call it at all. It's a statement returning the
          >function foo, but since you're not doing anything with the value it's
          >a useless statement.
          >>
          >A contrived example of foo; is:
          >>
          >var bar = foo; // assign function foo to bar
          >bar(); // call the function.
          >>
          >IOW, you do need the parentheses if you want to call a function.
          >[...]
          >
          Thanks for clearing that up. :)
          I really wonder what is so difficult about trying something out before
          asking (or to read the available reference material, including this
          newsgroup's FAQ, for that matter). If you are this afraid that code that
          you write could have effects this dangerous on the computer you are
          developing on, you should forget all about programming and computer science,
          and move to a happy little island nowhere near any technology.

          <http://jibbering.com/faq/>
          <http://catb.org/~esr/faqs/smart-questions.html>


          PointedEars, shaking his head
          --
          Anyone who slaps a 'this page is best viewed with Browser X' label on
          a Web page appears to be yearning for the bad old days, before the Web,
          when you had very little chance of reading a document written on another
          computer, another word processor, or another network. -- Tim Berners-Lee

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: Calling a function when passing no parameters

            Yansky wrote:
            Oh wait I have one more question.
            You don't say.
            What about when you're calling the function inside an event listener?
            e.g.
            function foo(){
            >
            alert('hi');
            >
            }
            window.addEvent Listener("load" , foo, false);
            `foo' refers to the (event) listener, which is the user-defined function.
            Again, the function is _not_ called here, but being referred to. It is
            called *later*, by the DOM implementation' s event handler, using the passed
            Function object reference.

            RTFM. RTFFAQ. STFW.


            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

            • Yansky

              #7
              Re: Calling a function when passing no parameters

              On Jul 18, 8:50 am, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
              wrote:
              Yansky wrote:
              On Jul 18, 8:12 am, Joost Diepenmaat <jo...@zeekat.n lwrote:
              Yansky <thegoodd...@gm ail.comwrites:
              >If I have the following function:
              >function foo(){
              >  alert('hi');
              >}
              >and I don't need to pass any parameters to it, is calling it this way:
              >foo;
              >the same as calling it this way:
              >foo();
              >
              That's probably the second most stupid question I have ever read on Usenet.
              w00t!

              Comment

              • Thomas 'PointedEars' Lahn

                #8
                Re: Calling a function when passing no parameters

                Yansky wrote:
                Thomas 'PointedEars' Lahn wrote:
                >Yansky wrote:
                >>On Jul 18, 8:12 am, Joost Diepenmaat <jo...@zeekat.n lwrote:
                >>>Yansky <thegoodd...@gm ail.comwrites:
                >>>>If I have the following function:
                >>>>function foo(){
                >>>> alert('hi');
                >>>>}
                >>>>and I don't need to pass any parameters to it, is calling it this way:
                >>>>foo;
                >>>>the same as calling it this way:
                >>>>foo();
                >That's probably the second most stupid question I have ever read on Usenet.
                >
                w00t!
                And that's definitely the most stupid reply I have ever read.

                Congratulations . Don't you want to apply for the Darwin Award as well?


                PointedEars
                --
                var bugRiddenCrashP ronePieceOfJunk = (
                navigator.userA gent.indexOf('M SIE 5') != -1
                && navigator.userA gent.indexOf('M ac') != -1
                ) // Plone, register_functi on.js:16

                Comment

                • Stevo

                  #9
                  Re: Calling a function when passing no parameters

                  Yansky wrote:
                  Oh wait I have one more question. What about when you're calling the
                  function inside an event listener? e.g.
                  function foo(){
                  alert('hi');
                  }
                  window.addEvent Listener("load" , foo, false);
                  That's fine, you're not calling it, you're nominating it as the function
                  that should be called.

                  Here, alias will be set to 3;

                  function foo(){return 3;};
                  var alias=foo();

                  Here, alias will become an actual alias of the function foo.

                  function foo(){return 3;}
                  var alias=foo;
                  //you can now do this:
                  alert(alias()); //alert 3

                  Comment

                  Working...