Results 1 to 4 of 4

Thread: Javascript (function() {}); and function() {};

  1. #1
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default Javascript (function() {}); and function() {};

    What's the difference between -

    Code:
    onload = (function() {
    
    });
    and

    Code:
    onload = function() {
    
    };
    Thanks, keyboard1333
    Last edited by keyboard; 09-13-2012 at 03:01 AM.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    When parentheses are not required, they are always optional (as far as I know). In other words, if you don't need parentheses, you can choose to add them if you wish.

    For example:
    if (((a==b))) {...

    They can sometimes make the code easier to read, or make sure that a statement isn't ambiguous. For example:
    if (a==b||c==d&&e==f) {...

    In that case, the code is valid, but it's unclear exactly how the logic works out. So adding some parentheses can help.

    Of course there are other cases where they're strictly required, such as when you're calling a function and adding arguments.



    In short, unless I'm missing something, those two are exactly equivalent.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

  4. #4
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    I tend to add parentheses, when it is something that I want to make clearer for readability, ie, in if statements:
    PHP Code:
    if($i == ($j / ($z + ($a 5))){

    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

Similar Threads

  1. Adding javascript function within ontoggle function
    By piers in forum Dynamic Drive scripts help
    Replies: 6
    Last Post: 07-22-2009, 02:41 AM
  2. Replies: 9
    Last Post: 09-16-2008, 05:31 PM
  3. Replies: 2
    Last Post: 08-02-2007, 06:19 AM
  4. out side of function or inside of function
    By riptide in forum JavaScript
    Replies: 2
    Last Post: 07-12-2007, 04:10 PM
  5. Passing Variables Function to Function
    By jscheuer1 in forum JavaScript
    Replies: 3
    Last Post: 03-26-2005, 12:22 AM

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
  •