6.7 Statements


An ECMA Script statement is similar to Java statements and it can be labeled by a "label set." ECMA Script supports the following types of statements: Block, VariableStatement, Empty-Statement, ExpressionStatement, IfStatement, IterationStatement, ContinueStatement, BreakStatement, ReturnStatement, WithStatement, LabelledStatement, SwitchStatement, ThrowStatement , and TryStatement .

The evaluation of a statement returns the tuple < type, completion, value, target > where type is the type of the statement evaluated, completion is the completion state which is either normal or throws an exception, value is the value returned by this statement, and target is the target for a branching statement often referring within a label set (mostly used by iterative statements).

6.7.1 Block

A block is either a single statement or a list of statements within curly braces { } , each of which terminates with a semi- colon . The evaluation of an empty block always returns the triplet <block, normal, empty, empty>. A block containing a list of statements is evaluated by evaluating the statements one by one according to the order in which they are specified. If during the evaluation of a statement an exception was thrown, no other statement in this block is evaluated, and the evaluation of this block returns <block, throw, exception, empty>; subsequent execution after the exception is thrown is normal. Note that a TryStatement may modify the value of target .

6.7.2 Variable Statement

A variable statement is a variable declaration list of the form var VariableDeclarationList . A VariableDeclarationList is either a VariableDeclaration or a VariableDeclarationList followed by a VariableDeclaration . A VariableDeclaration is an Identifier followed by an optional AssignmentExpression for initialization.

If a variable statement occurs inside a function declaration, the variables are defined with function-local scope in that function. Otherwise, they are defined with global scope and created as members of the global object.

Variables are created when the execution scope is entered. A Block does not define a new execution scope. Only ProgramDeclaration and FunctionDeclaration produce a new scope. Variables are initialized to Undefined when created. A variable with an initializer is assigned the value of its AssignmentExpression when the variable statement is executed, not when the variable is created. A variable statement is executed by evaluating the list of VariableDeclaration one by one. The evaluation of each VariableDeclaration always returns <variable, normal, V, empty> where 'V' is the initialized variable. If the valuation terminates abruptly, this should be indicated by the return tuple <variable, throw, exception, empty>.

6.7.3 Empty Statement

An ECMA Script empty statement is identical to an empty statement in Java. Its evaluation always returns <empty, normal, empty, empty>.

6.7.4 Expression Statement

This statement consists of an Expression and its evaluation always returns <expression, normal, getValue( evaluation of the Expression ) , empty>. For example, in case the Expression is an AssignmentExpression , this would be an assignment statement evaluated. If the valuation terminates abruptly, this should be indicated by the return tuple <expression, throw, exception, empty>. An expression statement cannot start with an opening curly brace because that makes it ambiguous with a Block, and it cannot start with the function keyword because that might make it ambiguous with a FunctionDeclaration .

6.7.5 The If Statement

An IfStatement is of the form if( Expression ) Statement , optionally followed by else Statement .

6.7.6 The Continue and Break Statements

A ContinueStatement and a BreakStatement comprise of the key words continue , break respectively, followed by an optional Identifier . An ECMA Script program is considered syntactically incorrect if any of the following are true:

  • The program contains a ContinueStatement without the optional Identifier , which is not nested, directly or indirectly (but not crossing function boundaries), within an IterationStatement .

  • The program contains a ContinueStatement with the optional Identifier , where Identifier does not appear in the label set of an enclosing (but not crossing function boundaries) IterationStatement .

  • The program contains a BreakStatement without the optional Identifier , which is not nested, directly or indirectly (but not crossing function boundaries), within an IterationStatement or a SwitchStatement .

  • The program contains a BreakStatement with the optional Identifier , where Identifier does not appear in the label set of an enclosing Statement (but not crossing function boundaries).

  • Continue and break statements are evaluated as part of the evaluation of Iterative statements.

6.7.7 Iteration Statements

An IterationStatement consists of a header, which consists of one of the do , while and for keywords and a parenthesized control construct, and a body, which consists of a Statement. The formal definition of an iteration statement is as follows , where the subscript opt denotes an optional expression:

  IterationStatement   :  do Statement while (Expression);   while (  Expression  )  Statement  for (  ExpressionNoIn  opt  ; Expression  opt  ; Expression  opt   )  Statement  for ( var  VariableDeclarationListNoIn; Expression  opt  ; Expression  opt   )  Statement  for (  LeftHandSideExpression  in  Expression  )  Statement  for ( var  VariableDeclarationNoIn  in  Expression  )  Statement  

where the expressions ExpressionNoIn, VariableDeclarationListNoIn are different from the Expression and VariableDeclaration in that the in (set membership) operator is excluded; they are identical in all other aspects.

6.7.8 The Return Statement

A ReturnStatement comprises of the return key word followed by an optional Expression . An ECMA Script program is considered syntactically incorrect if it contains a return statement that is not within a FunctionBody . A return statement causes a function to cease execution and return a value to the caller. If the return Expression is omitted, then the evaluation results in the tuple <return, normal, undefined, empty>. When the return Expression is specified, unless the evaluation of Expression terminates abruptly, then the evaluation results in the tuple <return, normal, getValue( Expression ), empty>. Otherwise, the evaluation produced an exception, then the evaluation results in the tuple <return, throw, exception, empty>.

6.7.9 The With Statement

A WithStatement comprises of the with key word, followed by ( , followed by an Expression , and followed by ) , and followed by a statement. This statement adds a computed object to the front of the scope chain of the current execution context, then executes a statement with this augmented scope chain, then restores the scope chain (see Table 6.22).

Table 6.22. Evaluating a with statement "with (Expression) Statement "

Step

Action

1

Evaluate the 'Expression'. If an exception occurred then return the result of this step.

2

Call GetValue( result of step 1 ) .

3

Call ToObject( result of step 2 ) .

4

Add the result of step 3 to the front of the scope chain.

5

Evaluate Statement using the augmented scope chain from step 4.

6

Let C be the result of step 5. If an exception was thrown in step 5, let C be the tuple <with, throw, exception, empty>; execution now proceeds as if no exception was thrown.

7

Remove the result of step 3 from the front of the scope chain.

8

Return C.

6.7.10 The Switch Statement

A SwitchStatement starts with key word switch , followed by ( , followed by an Expression, followed by ) , and followed by a CaseBlock . The latter starts with { followed by optional CaseClauses , followed by an optional DefaultClause , followed by optional additional CaseClauses , and terminated by } . CaseClauses are a sequence of expressions each starting with the key word case , followed by : and optionally followed by a list of statements. A DefaultClause starts with the key word default , followed by : , and optionally followed by a list of statements. The formal definition of a switch statement is as follows:

SwitchStatement : switch ( Expression ) CaseBlock

CaseBlock : { CaseClauses opt DefaultClause opt CaseClauses opt }

CaseClauses : CaseClauses opt case Expression : StatementList opt

DefaultClause : default : StatementList opt

The semantics of CaseBlock is very complex and its evaluation is challenging.

6.7.11 Labelled Statements

A LabelledStatement comprises of an Identifier followed by : followed by a statement. A Statement may be prefixed by a label. Labelled statements are only used in conjunction with labelled break and continue statements. There is no goto statement. An ECMA Script program is considered syntactically incorrect if it contains a LabelledStatement enclosed by a LabelledStatement with the same Identifier as label. This does not apply to labels appearing within the body of a FunctionDeclaration that is nested, directly or indirectly, within a LabelledStatement .

The expression Identifier : Statement is evaluated by adding Identifier to the label set of Statement and then evaluating Statement . If the LabelledStatement itself has a nonempty label set, these labels are also added to the label set of Statement before evaluating it. Prior to the evaluation of a LabelledStatement , the contained Statement is regarded as possessing an empty label set, except if it is an IterationStatement or a SwitchStatement , in which case it is regarded as possessing a label set consisting of the single element, empty.

6.7.12 The Throw Statement

A ThrowStatement comprises of the key word throw followed by an Expression with no line terminator in between. This statement is evaluated by first evaluating the Expression and, if no exception occurred during this evaluation, returning the tuple <throw, throw, getValue( Expression ), empty>. If an exception occurred during the evaluation of the Expression , then return the result of this evaluation instead of this tuple.

6.7.13 The Try-Catch-Finally Statement

The TryStatement encloses a block of code in which an exceptional condition can occur, such as a run-time error or a throw statement. A TryStatement comprises of the key word try followed by a Block , followed by either a single catch ( Identifier ) Block or a single finally Block or both. The catch clause provides the exception-handling code. When a catch clause catches an exception, its Identifier is bound to that exception (see Table 6.23).

Table 6.23. Evaluating a 'Try-Catch-Finally' Statement

Step

Action

1

Evaluate Block immediately following the try ; this produces a tuple <statement type, completion state, return value, target label>.

2

Let C be the result of step 1.

3

If there is no catch clause or the resulting completion state of step 1 is not 'throw' (i.e., normal termination), then go to step 6.

4

Evaluate the Block within catch clause with the return value from step 1 as the Identifier .

5

If the completion state of step 4 is abnormal, then let C be the result of step 4.

6

If there is no finally clause then return C.

7

Evaluate the Block within the finally .

8

If step 7 resulted in a normal completion state, then return C.

9

Return the result of step 7.



ITV Handbook. Technologies and Standards
ITV Handbook: Technologies and Standards
ISBN: 0131003127
EAN: 2147483647
Year: 2003
Pages: 170

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