Flow Control Constructs


This section details the flow control constructions that are available in JavaScript, the first of which is a building block for simplifying the grammar of other constructions.

Block Statements

While not really a flow control construct, the block statement allows many statements to be treated as one by enclosing them in curly braces:

 {  statements  } 

where statements is composed of zero or more valid JavaScript statements. Statements can always be grouped like this, as the body of a loop or function, or directly in the script, although a block only has its own local scope for functions.

The with Statement

  with  (  objectExpression  )  statement  

The object that objectExpression evaluates to is placed at the front of the scope chain while statement executes. Statements in statement can therefore utilize methods and properties of this object without explicitly using the property-accessing operator. An example of the with statement is shown here:

 with (document) {  write("hello ");  write("world ");  write("last modified on " + lastModified); } 

Functions

Primitive types are passed to functions by value. Composite types are passed by reference. Functions have their own local scope. Static scoping is employed.

  function   identifier  ( [  arg1  [,  arg2  [, ... ] ] ] )  {  statements  } 

From within a function you can return a value using the return statement:

return [ expression ];

If expression is omitted, the function returns undefined . A small example is shown here:

 function timesTwo(x) {  alert("x = "+x);  return x * 2; } result = timesTwo(3); alert(result); 

You can check how many arguments a function expects by accessing its length property:

functionName. length

The argument values, in addition to being placed in the declared parameters upon invocation, are accessible via the functionName .arguments[] array. This array holds the actual values passed to the function, so it may hold a different number of arguments than the function expects.

Conditionals

JavaScript supports the common if conditional, which has numerous forms:

if ( expression) statement

if ( expression) statement else statement

if (expression) statement else if (expression) statement

if (expression) statement else if (expression) statement else statement

An example if statement is demonstrated here:

 if (hand < 17)  alert('Better keep hitting'); else if ((hand >= 17) && (hand <= 21))        alert('Stand firm');       else          alert('Busted!'); 

Given the verbosity of a nested if statement, JavaScript, like many languages, supports the switch statement, whose syntax is

  switch  (  expression  )  {  case   val1  :  statement  [  break;  ]  case   val2  :  statement  [  break;  ]    ...  default  :  statement  } 

A simple switch statement is shown here:

 var ticket='First Class'; switch (ticket) {   case 'First Class': alert("Big Bucks ");                       break;   case 'Business': alert("Expensive, but worth it? ");                 break;   case 'Coach': alert("A little cramped but you made it.");                 break;   default: alert("Guess you can't afford to fly?"); } 

The break statement is used to exit the block associated with a switch and it must be included to avoid fall-through for the various cases that may be unintended . Omission of break may be purposeful, however, as it allows for the easy simulation of an or condition.

Loops

JavaScript supports the common loop forms including the following.

 1.  for  ( [  initStatement  ] ; [  logicalExpression  ] ; [  iterationStatement  ] )  statement  2.  while  (  expression  )  statement  3.  do   statement   while  (  expression  ); 

All three loops are demonstrated here:

 for (var i=0; i < 10; i++)    document.write(i+"<br />"); var i = 0; while (i < 10)  {    document.write(i+"<br />");    i++;  } var i = 0; do  {   document.write(i+"<br />");   i++; } while (i < 10) 

break and continue statements are commonly found in loop bodies and are discussed in the next section.

JavaScript also supports a modification of the for loop ( for/in ), which is useful for enumerating the properties of an object:

  for  ( [  var  ]  variable   in   objectExpression  )  statement  

This simple example here shows for/in being used to print out the properties of a browser s window.navigator object.

 for (var aProp in window.navigator)  document.write(aProp + "<br />"); 

Labeled Statements, Break, and Continue

Statements can be labeled using

label: statement

Jump to labeled statements in a block using either

break label;

or

continue label;

Otherwise:

  • break exits the loop, beginning execution following the loop body.

  • continue skips directly to the next iteration ( top ) of the loop.




JavaScript 2.0
JavaScript: The Complete Reference, Second Edition
ISBN: 0072253576
EAN: 2147483647
Year: 2004
Pages: 209

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net