Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question What is the use of this construct: (function() { some code })(); ?

    Hi there,

    I'm trying to better understand the Google Analytics asynchronous tracking code and am wondering why it uses the following construct: (function() { some code })();

    How is that different from some code?

    Thank you for your answers.
    (FYI: although I am familiar with dynamic programming languages, I'm new to Javascript.)

    Regards,
    Simon

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    972
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Quote Originally Posted by simonvdm View Post
    Hi there,

    I'm trying to better understand the Google Analytics asynchronous tracking code and am wondering why it uses the following construct: (function() { some code })();

    How is that different from some code?

    Thank you for your answers.
    (FYI: although I am familiar with dynamic programming languages, I'm new to Javascript.)

    Regards,
    Simon
    So basically its an anonymous function that calls itself ...

    (function(){alert("I called myself")})()

    Now that doen't seem very useful because

    alert("I called myself")

    does the same thing .

    Its usefullness usually is the fact
    that a "closure" is made in the function.
    Please google "closure".

  • #3
    Regular Coder
    Join Date
    Sep 2010
    Location
    Far far away
    Posts
    122
    Thanks
    0
    Thanked 16 Times in 16 Posts
    This construction is used in several cases.

    First of all -- an anonymous function is created and called immediately. Very useful when you need do something and keep the global scope off of some interim variables.

    Second one -- is closure. But this is not obvious case to demonstrate closures.

    Third -- obfuscating. Passing some global variables to this function is good practice to decrease size of the resulting file after obfuscating process.

    Quote Originally Posted by simonvdm View Post
    How is that different from some code?
    So, the answer for your question is the first description.

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Posts
    972
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Another use for...
    (function() { some code })();
    might be to return a function ...
    (function() { return someFunction})();

    maybe like this...



    PHP Code:
    <html>
    <
    body>

    <
    script type="text/javascript">
    addEvent = (function(){
     if (
    window.addEventListener){
        return function(
    objevTypefn){
               
    obj.addEventListener(evTypefnfalse);
               return 
    true;
            }}
    else if (
    window.attachEvent){
        return function(
    objevTypefn){
            var 
    obj.attachEvent("on"+evTypefn);
               return 
    r;
                     }}
    }
    )();

    alert(addEvent)

    </script>
    </body>
    </html> 

    in firefox and ie9 this will alert
    function(obj, evType, fn){
    obj.addEventListener(evType, fn, false);
    return true;
    }

    but in ie<9 it will alert
    function(obj, evType, fn){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
    }

  • #5
    Regular Coder
    Join Date
    Sep 2010
    Location
    Far far away
    Posts
    122
    Thanks
    0
    Thanked 16 Times in 16 Posts
    Agree with you, DaveyErwin.

    You have shown an example of an environment-dependent implementation. Depending on the actual environment (mostly browsers) the definite function will be assigned and executed.


  •  

    Tags for this Thread

    Posting Permissions

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