PDA

View Full Version : The { }'s


velious
11-14-2003, 05:47 AM
<script language="javascript">

var a = "my name is [name]";
{
document.writeln (a);
}

</script>


I cant remember the example but there was a script similar to this one that used curly braces even though there was no function.

In the script above, is it good practice to include the braces or not because they only make it more confusing?

adios
11-14-2003, 07:49 AM
Curlies, in addition to delimiting (bounding) a function's body, also are used to delimit a code 'block' as well. What is it? Just more than one statement that you'd like to be treated as a unit. Example:

if (color == 'red')
newcolor = 'green';
oldcolor = 'red';

In the above, the first statement after the conditional (newcolor = 'green';) will run if the condition evaluates to true, won't if it doesn't. The following statement (oldcolor = 'red';) will always be executed, even though it looks as if it's part of the conditional construction. Without curlies for grouping, the second statement stands alone and runs regardless.

if (color == 'red')
{
newcolor = 'green';
oldcolor = 'red';
}

...produces the desired effect. The same is true of for/while/do while loops:

for (n = 0; n < 33; ++n)
el = document.getElementById('layer' + n);
el.style.visibility = 'hidden';

Don't be surprised when only layer "layer32" gets hidden - the loop ran fully without ever getting to the visibility statement, until it was done.

In the example you posted, the curlies are completely unnecessary. They don't hurt anything, just signify nothing. Guessing you remembered it incorrectly....

Whether to use curlies when there's only one statement involved (no grouping issues) is a matter of taste; however, until you've really gotten your feet wet, I'd do it for the sake of consistency.

Danne
11-14-2003, 08:32 AM
Or maybe it was an example like this, you remembered:

var x = {
firstName : "Ford",
lastName : "Fairlane"
};


Which is an object, and the same thing as:

var x = new Object();
x.firstName = "Ford";
x.lastName = "Fairlane";

liorean
11-14-2003, 11:58 AM
Heh. This goes back to programming theory...

In JavaScript, there is a type of statement called the block statement. This is a way to put several statements into one single statement.

For example, the if statement looks like this:

- if(condition expression) statement;
Since the block statement is a kind of statement, you can place it there, to execute several statements at once. If the condition expression evaluates to false, the statement will not be executed.
- The same goes for all situations you may put a statement in, such as the for statement, the for..in statement, the while statement, the do..while statement, the if..else statement, and whatever else I forgot to mention...

Since the block statement is a type of statement, the characters used to delimit a block statement ('{' and '}') does not have the same meaning in an expression as it does in a statement. In an expression, they instead delimit an object literal, except for the special case of the function operator, which uses them to create a function literal;





It is good practice to not include the braces, but you should not let that prevent you from using them if you want them.




(There is an equivalent for expressions as well. The comma operator allows using multiple expressions in the place of one expression - you usually have to wrap the expression in parentheses however - where the return value of will be the return value of the last nested expression)

var t = (alert(1),2); // => t=2, alerts "1".

velious
11-14-2003, 08:09 PM
thanks everyone :)