SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2006
    Posts
    153
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Putting parentheses around an entire anonymous function, please explain?

    Hi
    I've been reading a book which gets Amazon.com's highest user rating, but is said to be for the sophisticated reader. I fear my sophistication is lacking in regard to one of the code examples, however so far I would agree with the Amazon rating. So if you can indulge me and explain one of the coding examples I would be most appreciative. The name of the book is "Pro JavaScript Techniques" and the code example I am having trouble with is immediately below.
    Code:
    // Create a new anonymous function, to use as a wrapper
    (function(){
        // The variable that would, normally, be global
        var msg = "Thanks for visiting!";
    
        // Binding a new function to a global object
        window.onunload = function(){
            // Which uses the 'hidden' variable
            alert( msg );
        };
    
    // Close off the anonymous function and execute it
    }());
    This code is from the book site. The code in my e-book appears a little differently but I am assuming the book site is what he intended. In my e-book the author says this about the code above.
    There’s another, common, JavaScript-coding problem that closures can solve. New
    JavaScript developers tend to accidentally leave a lot of extra variables sitting in the global
    scope. This is generally considered to be bad practice, as those extra variables could quietly
    interfere with other libraries, causing confusing problems to occur. Using a self-executing,
    anonymous function you can essentially hide all normally global variables from being seen
    by other code, as shown in Listing 2-15.
    Well that sounds very good but how do I call this function? I mean those parentheses around the whole function in this coding example is confusing me. What is the purpose of surrounding this function with parentheses? Please say what you can about this. His concept seems good if I knew how to get it to work!

    Sincerely
    Marc

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    its an anonymous function, you cannot call it. Note that it is wrapped within (function(){})();, meaning it will be called once the JavaScript loads. Parentheses are there to call it.

  3. #3
    SitePoint Zealot
    Join Date
    Oct 2006
    Posts
    153
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi
    I am finding the actual behavior of my computer is diverging from what was said immediately above. The script that I used to try which, I am posting a median below these words, just has parentheses and scripttag around it. In accordance with what was stated above I thought it should be executed on load of the page. However it executes on reload in both Internet Explorer and Firefox. It does not execute onload. Any further explanation or a link that would explain how this in parentheses stuff works would be appreciated.
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD 
    XHTML 1.0 Strict//EN" "
    http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    <head>
    	<meta http-equiv="Content-Type"
    	 content="text/html; charset=iso-8859-1" />
    	<title>Create a new anonymous 
    	function, to use as a wrapper</title>
    <script type="text/javascript" charset="utf-8">
    // Create a new anonymous function, to use as a wrapper
    (function(){
        // The variable that would, normally, be global
        var msg = "Thanks for visiting!";
    
        // Binding a new function to a global object
        window.onunload = function(){
            // Which uses the 'hidden' variable
            alert( msg );
        };
    
    // Close off the anonymous function and execute it
    }());
    </script>
    </head>
    <body>
    <h1>Create a new anonymous 
    function, to use as a wrapper</h1>	
    </body>
    </html>

  4. #4
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Reason you get the alert on the reload is because you are using window.onunload. Try alerting something right after the msg variable declaration and you will notice it will trigger as the page loads.

  5. #5
    SitePoint Zealot
    Join Date
    Oct 2006
    Posts
    153
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you I did not see that


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •