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

Re: [ydn-javascript] Unfamiliar Javascript function syntax

Expand Messages
  • Dav Glass
    Randy -- What you are seeing is a common structure in YUI to grant us the ability to have private variables :) That was a mouth full, try this: alert(me);
    Message 1 of 3 , Jun 1, 2008
    • 0 Attachment
      Randy --


      What you are seeing is a common structure in YUI to grant us the ability to have private variables :)
      That was a mouth full, try this:

      alert(me); //This will be 'undefined';

      (function() {
      var me = 'This is me..';
      alert(me); //This will be 'This is me..'

      //Now in here we can use var to scope any variable or function to this "anonymous function"
      })(); //The extra ()'s here make the anonymous function execute instantly..

      alert(me); //This will still be 'undefined';


      More reading on this stuff:
      http://yuiblog.com/blog/2008/05/24/durable-objects/
      http://yuiblog.com/blog/2007/06/12/module-pattern/
      http://www.crockford.com/javascript/private.html
      http://www.crockford.com/

      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: Randy Weber <randyweb@...>
      To: ydn-javascript@yahoogroups.com
      Sent: Sunday, June 1, 2008 12:17:24 PM
      Subject: [ydn-javascript] Unfamiliar Javascript function syntax

      I am a novice self-taught javascript programmer charged with a complex
      project. Though I have plenty of programming experience, I am
      struggling with some aspects of javascript.

      I am using YUI (a wonderful find) to save time and help me learn
      advanced javascript techniques, so pardon me if this isn't the right
      place to post my question.

      I see the use of function in a way that is unfamiliar to me:
      (function() {
      var Dom = YAHOO.util.Dom,
      Event = YAHOO.util.Event;
      })();

      What is unfamiliar is the preceding parenthesis. It appears to me that
      the function runs before the load event. Is that all there is to the
      preceding parenthesis or is there more?

      Can someone tell me or refer me to a link that will tell me what is
      happening here? When I search the web, I get a myriad of examples of
      simpler function calls which I already know. This appears to be quite
      different than a function which simply returns a value.

      Also, what is the purpose of the final ().

      Thanks in advance

      Randy Weber



      ------------------------------------

      Yahoo! Groups Links
    • Randy Weber
      Dav -- Thanks. It s starting to make sense now. Good reading by crockford, too. Randy ... ability to have private variables :) ... function to this anonymous
      Message 2 of 3 , Jun 3, 2008
      • 0 Attachment
        Dav --

        Thanks. It's starting to make sense now. Good reading by crockford,
        too.

        Randy

        --- In ydn-javascript@yahoogroups.com, Dav Glass <dav.glass@...>
        wrote:
        >
        > Randy --
        >
        >
        > What you are seeing is a common structure in YUI to grant us the
        ability to have private variables :)
        > That was a mouth full, try this:
        >
        > alert(me); //This will be 'undefined';
        >
        > (function() {
        > var me = 'This is me..';
        > alert(me); //This will be 'This is me..'
        >
        > //Now in here we can use var to scope any variable or
        function to this "anonymous function"
        > })(); //The extra ()'s here make the anonymous function execute
        instantly..
        >
        > alert(me); //This will still be 'undefined';
        >
        >
        > More reading on this stuff:
        > http://yuiblog.com/blog/2008/05/24/durable-objects/
        > http://yuiblog.com/blog/2007/06/12/module-pattern/
        > http://www.crockford.com/javascript/private.html
        > http://www.crockford.com/
        >
        > 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: Randy Weber <randyweb@...>
        > To: ydn-javascript@yahoogroups.com
        > Sent: Sunday, June 1, 2008 12:17:24 PM
        > Subject: [ydn-javascript] Unfamiliar Javascript function syntax
        >
        > I am a novice self-taught javascript programmer charged with a
        complex
        > project. Though I have plenty of programming experience, I am
        > struggling with some aspects of javascript.
        >
        > I am using YUI (a wonderful find) to save time and help me learn
        > advanced javascript techniques, so pardon me if this isn't the right
        > place to post my question.
        >
        > I see the use of function in a way that is unfamiliar to me:
        > (function() {
        > var Dom = YAHOO.util.Dom,
        > Event = YAHOO.util.Event;
        > })();
        >
        > What is unfamiliar is the preceding parenthesis. It appears to me
        that
        > the function runs before the load event. Is that all there is to the
        > preceding parenthesis or is there more?
        >
        > Can someone tell me or refer me to a link that will tell me what is
        > happening here? When I search the web, I get a myriad of examples of
        > simpler function calls which I already know. This appears to be
        quite
        > different than a function which simply returns a value.
        >
        > Also, what is the purpose of the final ().
        >
        > Thanks in advance
        >
        > Randy Weber
        >
        >
        >
        > ------------------------------------
        >
        > Yahoo! Groups Links
        >
      Your message has been successfully submitted and would be delivered to recipients shortly.