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

Re: (function() { -- what? why?

Expand Messages
  • tssha
    ... get ... Look at ... (function() {}()); This is a self-executing, anonymous function. This syntax results in the function being executed immediately after
    Message 1 of 3 , Aug 1, 2007
      --- In ydn-javascript@yahoogroups.com, Ian Shafer <ian@...> wrote:
      >
      > Hello,
      >
      > I'm an experienced Java (not Javascript) developer who's starting to
      get
      > into Javascript. I've noticed this code:
      >
      > (function() {
      >
      > starting off classes that extend other classes.
      >
      > For example, take a look at
      > http://developer.yahoo.com/yui/examples/dragdrop/dd-groups.html.
      Look at
      > the definition of
      > YAHOO.example.DDPlayer.
      >
      > Can someone explain what this code does? Thanks,

      (function() {}());

      This is a self-executing, anonymous function. This syntax results in
      the function being executed immediately after definition.

      Regards,
      Thomas
    • dav.glass@yahoo.com
      To add to Thomas post.. This syntax is very handy for making private variables.. like this: (function() { var Dom = YAHOO.util.Dom, Event = YAHOO.util.Event,
      Message 2 of 3 , Aug 1, 2007
        To add to Thomas' post.. This syntax is very handy for making private variables.. like this:

        (function() {
            var Dom = YAHOO.util.Dom,
                  Event = YAHOO.util.Event,
                  test = null;

            function one() {
               test = Dom.get('someId');
            }
            function two() {
               var id = test.id;
            }
        })();

        You will notice that Dom, Event and test are scoped to the anonymous function and not the window..

        It helps to keep the global scope less crowded..

        Hope that helps :)
        Dav
         
        Dav Glass
        dav.glass@...
        blog.davglass.com
         
         
        + Windows: n. - The most successful computer virus, ever. +
        + A computer without a Microsoft operating system is like a dog
        without bricks tied to its head +
        + A Microsoft Certified Systems Engineer is to computing what a
        McDonalds Certified Food Specialist is to fine cuisine +


        ----- Original Message ----
        From: tssha <tsha@...>
        To: ydn-javascript@yahoogroups.com
        Sent: Wednesday, August 1, 2007 2:48:47 PM
        Subject: [ydn-javascript] Re: (function() { -- what? why?

        --- In ydn-javascript@ yahoogroups. com, Ian Shafer <ian@...> wrote:
        >
        > Hello,
        >
        > I'm an experienced Java (not Javascript) developer who's starting to
        get
        > into Javascript. I've noticed this code:
        >
        > (function() {
        >
        > starting off classes that extend other classes.
        >
        > For example, take a look at
        > http://developer. yahoo.com/ yui/examples/ dragdrop/ dd-groups. html.
        Look at
        > the definition of
        > YAHOO.example. DDPlayer.
        >
        > Can someone explain what this code does? Thanks,

        (function() {}());

        This is a self-executing, anonymous function. This syntax results in
        the function being executed immediately after definition.

        Regards,
        Thomas


      Your message has been successfully submitted and would be delivered to recipients shortly.