SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    Denver
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What is the purpose of writing a function like this

    What is the purpose of writing a function inside parentheses?
    PHP Code:
    (function()
     {
         var 
    myvar xxx;
         
    // my function code here....
     
    }

    )() 
    thanks.

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is executed immediately.
    Birnam wood is come to Dunsinane

  3. #3
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    +++
    Divide By Cucumber Error. Please Reinstall Universe And Reboot
    +++

  4. #4
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    Denver
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    AutisticCuckoo, are you meaning on page load? I mean, is this any different from simply having the code outside function()?

  5. #5
    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)
    It will not wait for page load, it will execute immediatetly when found. Variables defined within this self-executing anonymous function will also not be global. Meaning no pollution.

  6. #6
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @cbiti: yes, the code will execute as soon as the script is read and parsed. That will probably be before the entire page has been loaded, unless the script is at the very end.

    The only difference I can think of from having the code outside function() is that you will not pollute the global namespace with any variable declarations you make inside the function.

    @MikeFoster:
    (Does your computer have an 'Anthill Inside' sticker on it?)
    Birnam wood is come to Dunsinane

  7. #7
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    Denver
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks very much to all of you guys.

  8. #8
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AutisticCuckoo
    (Does your computer have an 'Anthill Inside' sticker on it?)
    LOL!

    I have to admit that I only recently learned of Mr. Pratchett - in fact, because of your sig!

    It gets worse. I have, before now, waited for a pen to perform a macro.
    -- (Terry Pratchett, alt.fan.pratchett)

  9. #9
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    813
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I rarely write anonymous function that execute immediately in this way. I just wonder how different is it with using Function


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
  •