javascript programming idiom

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

    javascript programming idiom

    HI all:

    I often see people write code like as below to invoke code block:

    (
    function() { do some thing here}
    ) ()

    my question is why do like this, and what is benefit.

    regards
  • Stevo

    #2
    Re: javascript programming idiom

    ctx2002 wrote:
    HI all:
    I often see people write code like as below to invoke code block:
    (
    function() { do some thing here}
    ) ()
    my question is why do like this, and what is benefit.
    regards
    It can help keep the global namespace clean. It's defining an anonymouse
    function and calling it immediately, and unless some kind of closure is
    created, it will be discarded. All variables defined inside the function
    are lost with it. For example, if you wanted to do this following code
    INLINE:

    var x=2;
    var y=3;
    var z=x*y;
    alert(z);

    You've multipled 2 by 3 and alerted 6. But behind you, you've left (and
    possibly overwritten if they already existed) global variables x, y and
    z. Where as this code below leaves/overwrites nothing:

    (function(){var x=2;var y=3;var z=x*y;alert(z); })()

    Same result, less pollution.

    It can be just people showing off how many JS tricks they know though.

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: javascript programming idiom

      ctx2002 wrote:
      I often see people write code like as below to invoke code block:
      >
      (
      function() { do some thing here}
      ) ()
      >
      my question is why do like this, and what is benefit.
      ^
      My question -- which is actually a *question* -- is far more simple: Why
      don't you use the newsgroup's archive, readily available through the Web
      interface you are just bathing your hands in? This has been discussed ad
      nauseam here already.




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

      Comment

      • ctx2002

        #4
        Re: javascript programming idiom

        hi Thomas:

        can you be a bit nice to other people?
        i have searched javascript FAQ, and did not found any answer.
        not every one is a searching guru like you.

        regards

        On May 30, 12:40 pm, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
        wrote:
        ctx2002 wrote:
        I often see people write code like as below to invoke code block:
        >
        (
         function() { do some thing here}
        ) ()
        >
        my question is why do like this, and what is benefit.
        >
                                                              ^
        My question -- which is actually a *question* -- is far more simple: Why
        don't you use the newsgroup's archive, readily available through the Web
        interface you are just bathing your hands in?  This has been discussed ad
        nauseam here already.
        >

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

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: javascript programming idiom

          ctx2002 wrote:
          hi Thomas:
          This is NetNews, not private e-mail.
          can you be a bit nice to other people?
          Can you behave a bit less like a semi-literate idiot?


          [snipped top post]
          Score adjusted

          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

          • dhtml

            #6
            Re: javascript programming idiom

            On May 30, 4:18 am, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
            wrote:
            ctx2002 wrote:
            can you be a bit nice to other people?
            >
            Can you behave a bit less like a semi-literate idiot?
            >
            Part of the problem I have with these posts of yours is that it
            discourages people from asking questions.

            I have no doubt that you can be rude and obnoxious. Anyone can see
            this by "searching the archives."

            You've also got some pretty good knowledge and your responses can be
            helpful.

            In the OP's question, he might not know even what the term 'closure'
            means or where to start with trying to figure out the coding construct
            that he saw. In that case, you could point him to Richard's article on
            Jibbering.



            Garrett

            PointedEars

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: javascript programming idiom

              dhtml wrote:
              In the OP's question, he might not know even what the term 'closure'
              means or where to start with trying to figure out the coding construct
              that he saw. In that case, you could point him to Richard's article on
              Jibbering.
              >
              http://www.jibbering.com/faq/faq_notes/closures.html
              Or I could point them to the FAQ which refers to this article (at least it
              should) and in addition provides a bunch of useful information on how to
              improve his posting style, finally earning themselves the answers they
              desire. BTDT.

              They did not even consider to copy-paste and experiment with the code.
              Nuff said.


              F'up2 poster

              PointedEars
              --
              Use any version of Microsoft Frontpage to create your site.
              (This won't prevent people from viewing your source, but no one
              will want to steal it.)
              -- from <http://www.vortex-webdesign.com/help/hidesource.htm>

              Comment

              • Dr J R Stockton

                #8
                Re: javascript programming idiom

                In comp.lang.javas cript message <ee65eb53-8812-466c-8ad6-169af94c474d@p3
                9g2000prm.googl egroups.com>, Thu, 29 May 2008 18:45:33, ctx2002
                <ctx2002@gmail. composted:
                >hi Thomas:
                >
                >can you be a bit nice to other people?
                The evidence is pretty clear : it is not a concept that he is capable of
                understanding.

                --
                (c) John Stockton, nr London, UK. ?@merlyn.demon. co.uk Turnpike v6.05 MIME.
                Web <URL:http://www.merlyn.demo n.co.uk/- FAQish topics, acronyms, & links.
                Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
                Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)

                Comment

                • Jorge

                  #9
                  Re: javascript programming idiom

                  On May 30, 11:43 pm, dhtml <dhtmlkitc...@g mail.comwrote:
                  In that case, you could point him to Richard's article on
                  Jibbering.
                  >

                  >
                  This article has a typo in this paragraph :

                  The first object in the chain is ActInner1 and it has a property named
                  innerArg with the value 2. All of the other 3 identifiers correspond
                  with named properties of ActOuter1; arg1 is 2, arg2 is 4 and localVar
                  is 8. The function call returns ((2 + 2)/(2 + 8)).

                  Tha last sentence should read : "The function call returns ((2 + 4)/(2
                  + 8))."

                  --Jorge.

                  Comment

                  Working...