Results 1 to 3 of 3

Thread: conditions without brackets

  1. #1
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default conditions without brackets

    ive seen code written like this
    Code:
    if(jQuery.support.opacity)
    	$('div.box-5').find('div.movebox-content *').fadeTo(200, 1);
    else
    	$('div.box-5').find('div.movebox-content *').css("visibility","visible");
    	$(document).trigger("newBackground",[nI]);
    shouldnt it be like this?
    Code:
    if(jQuery.support.opacity){
    	$('div.box-5').find('div.movebox-content *').fadeTo(200, 1);
    }else{
    	$('div.box-5').find('div.movebox-content *').css("visibility","visible");
    	$(document).trigger("newBackground",[nI]);
    }
    but obviously it works without the brackets, can anyone tell me whats going on?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,920
    Thanks
    43
    Thanked 3,187 Times in 3,151 Posts
    Blog Entries
    12

    Default

    To be technically perfect, javascript like HTML needs to follow certain syntax rules. However, browsers will error correct for a lot.

    These technicalities become increasingly important if one wants to compress and/or encrypt ones code. And if code is being imported via the responseText or responseXML of an AJAX or other server request.

    In your example though the two versions are not equivalent. If brackets are omitted, only the immediately following directive is a part of the conditional. So

    Code:
    if(jQuery.support.opacity)
    	$('div.box-5').find('div.movebox-content *').fadeTo(200, 1);
    else
    	$('div.box-5').find('div.movebox-content *').css("visibility","visible");
    	$(document).trigger("newBackground",[nI]);
    is equivalent to:

    Code:
    if(jQuery.support.opacity){
    	$('div.box-5').find('div.movebox-content *').fadeTo(200, 1);
    } else {
    	$('div.box-5').find('div.movebox-content *').css("visibility","visible");
    }
    $(document).trigger("newBackground",[nI]);
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    The braces {} group line(s) as a unit. This means that you can group 2 or more lines as, effectively, a single line (or a single "operation").

    if (condition) function();

    That's perfectly fine, because if (condition) RESULT. But to make RESULT complex, or span multiple lines, the braces allow for something other than just the default one line item.
    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

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
  •