Loading ...
Sorry, an error occurred while loading the content.

self-invoking functions

Expand Messages
  • shortestpath
    according to dustin diaz (http://www.dustindiaz.com/awkward-looking-javascript/), a self-invoking function looks like this: (function() { ... }()); but
    Message 1 of 5 , Feb 14, 2008
    • 0 Attachment
      according to dustin diaz
      (http://www.dustindiaz.com/awkward-looking-javascript/), a
      self-invoking function looks like this:

      (function() {
      ...
      }());

      but according to pat
      (http://www.hunlock.com/blogs/Functional_Javascript), a self-invoking
      function looks like this:

      (function() {
      ...
      })();

      could someone explain the difference? tia...
    • liorean
      ... There is no difference. The only reason you need to wrap the function ...(...){...} part in the first place is that the function keyword in a statement
      Message 2 of 5 , Feb 15, 2008
      • 0 Attachment
        On 15/02/2008, shortestpath <no_reply@yahoogroups.com> wrote:
        > according to dustin diaz
        > (http://www.dustindiaz.com/awkward-looking-javascript/), a
        > self-invoking function looks like this:
        >
        > (function() {
        > ...
        > }());
        >
        > but according to pat
        > (http://www.hunlock.com/blogs/Functional_Javascript), a self-invoking
        > function looks like this:
        >
        > (function() {
        > ...
        > })();
        >
        > could someone explain the difference? tia...

        There is no difference. The only reason you need to wrap the "function
        ...(...){...}" part in the first place is that the "function" keyword
        in a statement context is a function declaration, while what you want
        is a function expression. The parentheses are there to force treating
        it as a function expression instead.

        If you already are in expression context, you wouldn't need the parentheses.
        --
        David "liorean" Andersson
      • David Smart
        Gut feel says that Diaz is wrong. It seems to be corrected in the discussion lower down in his article, and other Google hits I looked at has the final
        Message 3 of 5 , Feb 15, 2008
        • 0 Attachment
          Gut feel says that Diaz is wrong. It seems to be corrected in the
          discussion lower down in his article, and other Google hits I looked at has
          the final calling-parameters construct outside the parentheses.

          However, I haven't tried either and haven't actually used them at all. I
          suppose I'm too much of a traditionalist at heart. :-)

          Regards, Dave S

          ----- Original Message -----
          From: "shortestpath" <no_reply@yahoogroups.com>
          To: <JavaScript_Official@yahoogroups.com>
          Sent: Friday, February 15, 2008 4:15 PM
          Subject: [JavaScript] self-invoking functions


          > according to dustin diaz
          > (http://www.dustindiaz.com/awkward-looking-javascript/), a
          > self-invoking function looks like this:
          >
          > (function() {
          > ...
          > }());
          >
          > but according to pat
          > (http://www.hunlock.com/blogs/Functional_Javascript), a self-invoking
          > function looks like this:
          >
          > (function() {
          > ...
          > })();
          >
          > could someone explain the difference? tia...
          >
          >
          >
          > Visit http://aiaiai.com for more groups to join
          > Yahoo! Groups Links
          >
          >
          >
        • liorean
          ... No, it s just two different ways of achieving the same thing. (function(){})() Here you have one wrapping parenthesis to turn the context into an
          Message 4 of 5 , Mar 2, 2008
          • 0 Attachment
            On 15/02/2008, David Smart <smartware.consulting@...> wrote:
            > Gut feel says that Diaz is wrong. It seems to be corrected in the
            > discussion lower down in his article, and other Google hits I looked at has
            > the final calling-parameters construct outside the parentheses.

            No, it's just two different ways of achieving the same thing.

            (function(){})()

            Here you have one wrapping parenthesis to turn the context into an
            expression. The function object is returned from the paren-expression,
            and is then called.

            (function(){}())

            Here you have one wrapping parenthesis to turn the context into an
            expression. The function object is created and called within the
            parens, and then the result is returned from the paren-expression.

            Both ways of writing it do the exact same things, just in a slightly
            different order. I wouldn't be surprised if most ECMAScript engines
            compiled it into the exact same bytecode.

            > However, I haven't tried either and haven't actually used them at all. I
            > suppose I'm too much of a traditionalist at heart. :-)

            They're one of the most useful abstractions around, since closures are
            the only method we have of keeping data private in JavaScript.
            Granted, you can have closures without making them anonymous. Having
            them anonymous gives you the chance of setting something up which can
            not easily be hijacked, however.
            --
            David "liorean" Andersson
          • David Smart
            David Thanks for that explanation. I must have a closer look at self-invoking functions to see where I d use them in the things I do. Regards, Dave S ...
            Message 5 of 5 , Mar 3, 2008
            • 0 Attachment
              David

              Thanks for that explanation. I must have a closer look at self-invoking
              functions to see where I'd use them in the things I do.

              Regards, Dave S

              ----- Original Message -----
              From: "liorean" <liorean@...>
              To: <JavaScript_Official@yahoogroups.com>
              Sent: Monday, March 03, 2008 8:07 AM
              Subject: Re: [JavaScript] self-invoking functions


              > On 15/02/2008, David Smart <smartware.consulting@...> wrote:
              >> Gut feel says that Diaz is wrong. It seems to be corrected in the
              >> discussion lower down in his article, and other Google hits I looked at
              >> has
              >> the final calling-parameters construct outside the parentheses.
              >
              > No, it's just two different ways of achieving the same thing.
              >
              > (function(){})()
              >
              > Here you have one wrapping parenthesis to turn the context into an
              > expression. The function object is returned from the paren-expression,
              > and is then called.
              >
              > (function(){}())
              >
              > Here you have one wrapping parenthesis to turn the context into an
              > expression. The function object is created and called within the
              > parens, and then the result is returned from the paren-expression.
              >
              > Both ways of writing it do the exact same things, just in a slightly
              > different order. I wouldn't be surprised if most ECMAScript engines
              > compiled it into the exact same bytecode.
              >
              >> However, I haven't tried either and haven't actually used them at all.
              >> I
              >> suppose I'm too much of a traditionalist at heart. :-)
              >
              > They're one of the most useful abstractions around, since closures are
              > the only method we have of keeping data private in JavaScript.
              > Granted, you can have closures without making them anonymous. Having
              > them anonymous gives you the chance of setting something up which can
              > not easily be hijacked, however.
              > --
              > David "liorean" Andersson
              >
              >
              > Visit http://aiaiai.com for more groups to join
              > Yahoo! Groups Links
              >
              >
              >
            Your message has been successfully submitted and would be delivered to recipients shortly.