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 BlockA 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 StatementA 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 StatementAn ECMA Script empty statement is identical to an empty statement in Java. Its evaluation always returns <empty, normal, empty, empty>. 6.7.4 Expression StatementThis 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 StatementAn IfStatement is of the form if( Expression ) Statement , optionally followed by else Statement . 6.7.6 The Continue and Break StatementsA 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:
6.7.7 Iteration StatementsAn 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 StatementA 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 StatementA 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 "
6.7.10 The Switch StatementA 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:
The semantics of CaseBlock is very complex and its evaluation is challenging. 6.7.11 Labelled StatementsA 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 StatementA 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 StatementThe 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
|