About (function(){})()

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

    About (function(){})()

    It has been widely used nowadays to wrap JavaScript libraries, as seen
    in JQuery.

    (function() {
    var global = this;
    // ...
    })();

    The advantage, as I learned, is the isolation from other global
    variables. But how? What other advantages does this wrapper have. For
    example, I have my library as:

    var myLib = {
    go : function(){
    alert('Hah')
    },
    arr : new Array()
    }

    How would I write myLib in (function(){})( ) format? Do I need to? What
    are the advantages?
    Please clear for me if you can.
    Thanks
  • David Mark

    #2
    Re: About (function(){})( )

    On Oct 29, 9:46 am, vunet <vunet...@gmail .comwrote:
    It has been widely used nowadays to wrap JavaScript libraries, as seen
    in JQuery.
    Seen long before jQuery.
    >
    (function() {
      var global = this;
      // ...
    >
    })();
    Actually, that looks like my library.
    >
    The advantage, as I learned, is the isolation from other global
    variables. But how? What other advantages does this wrapper have. For
    example, I have my library as:
    How what? As for other advantages, "minifiers" can be more aggressive
    with this pattern.
    >
    var myLib = {
      go : function(){
        alert('Hah')
      },
      arr : new Array()
    >
    }
    >
    How would I write myLib in (function(){})( ) format? Do I need to? What
    It is hard to say what you need to do. The sample above doesn't
    indicate what you are trying to do.
    are the advantages?
    For one, you can have protected members, which you cannot do with an
    object literal alone.

    [snip]

    Comment

    • hj

      #3
      Re: About (function(){})( )

      On Oct 29, 6:46 am, vunet <vunet...@gmail .comwrote:
      It has been widely used nowadays to wrap JavaScript libraries, as seen
      in JQuery.
      >
      (function() {
      var global = this;
      // ...
      >
      })();
      >
      The advantage, as I learned, is the isolation from other global
      variables. But how? What other advantages does this wrapper have. For
      example, I have my library as:
      Here's a concrete example: Imagine that you have some functions to
      declare, *and*
      some code that you want to be executed, e.g.:


      function x() {
      // Do something
      }
      function y(a) {
      // Do something else
      }
      function untilSometime() {
      x();
      if ((now.getTime() + 1000) < (new Date().getTime( ))) {
      y();
      } else {
      clearInterval(i handle);
      }
      }
      var now = new Date;
      var ihandle = setInterval(unt ilSometime,100) ;


      The result is that "x", "y", "untilSometime" , "now", and "ihandle" are
      all
      global properties. There's a possibility that any of them have
      overwritten
      pre-existing properties with the same names, as well as the
      possibility that
      they'll be overwritten by code later in the document. Wrapping the
      whole
      thing in an immediately executed function expression makes all those
      local
      to that function, and avoids both those problems; and produces the
      desired
      effect without creating -any- global properties.

      --

      hj

      Comment

      • Rik Wasmus

        #4
        Re: About (function(){})( )

        On Fri, 31 Oct 2008 07:18:02 +0100, RobG <rgqld@iinet.ne t.auwrote:
        On Oct 30, 11:55 pm, vunet <vunet...@gmail .comwrote:
        [...]
        >Thank you for very descriptive answer. Can I possible clear out about
        >what happens when this is excecuted:
        >>
        >(function() {
        > // declare myLib
        > var myLib = {...};
        >>
        > // now use it.
        > myLib.go(...);
        > // and so on
        >>
        >})();
        >>
        >The code is executed on JS run-time and is hidden to any other
        >libraries or variables because of the wrapper function. But at the
        >same time that code cannot be accessed from outside if I have onClick
        >handler to run one of the functions inside like onClick="myLib. go()".
        >Is this more "robust" (as you say) because it is global:
        > var global = (function(){ret urn this})();
        >
        No. I suggested it because that method of getting a reference to the
        global object can be used anywhere, you don’t have to run it as global
        code.
        >
        Using an anonymous function wrapper around your library is not
        necessarily more robust (depending on what you think that means), in
        fact it can make life more difficult as you have to add listeners
        dynamically, which introduces a whole new set of issues. If it is
        purely to avoid naming clashes, an good technique is to use a prefix
        for your functions, e.g.
        >
        function vU_go() { ... }
        function vU_stop() { ... }
        >
        And so on. That method provides about the same protection as the
        object:property method of:
        >
        var vU = {
        go: function() {...}
        stop: function() {...}
        }
        >
        If you can get away with simple listeners like:
        >
        <input type=”buttonâ € onclick=”vU_g o()”>
        Hmmm, and prevent 'smart' applications from converting your carefully used
        '"'s to '”'....

        (=94 =decimal 148 in the windows-1252/quoted-printable source)

        /prays to a random deity for this post to make sense when sent, I don't
        even trust my nntp client all that much...
        --
        Rik

        Comment

        Working...