JavaScript 2.0
Core Language
Statements
previousupnext

Thursday, November 11, 1999

Most of the behavior of statements is the same as in JavaScript 1.5. Differences are highlighted below.

w  {abbrevabbrevNonEmptyabbrevNoShortIffull}
TopStatementw 
   Statementw
|  LanguageDeclarationw
Statementw 
   AnnotatedDefinitionw
|  EmptyStatementw
|  ExpressionStatement Semicolonw
|  AnnotatedBlock
|  LabeledStatementw
|  IfStatementw
|  SwitchStatement
|  DoStatement Semicolonw
|  WhileStatementw
|  ForStatementw
|  WithStatementw
|  ContinueStatement Semicolonw
|  BreakStatement Semicolonw
|  ReturnStatement Semicolonw
|  ThrowStatement Semicolonw
|  TryStatement
Semicolonabbrev 
   ;
|  VirtualSemicolon
|  «empty»
SemicolonabbrevNonEmpty 
   ;
|  VirtualSemicolon
|  «empty»
SemicolonabbrevNoShortIf 
   ;
|  VirtualSemicolon
|  «empty»
Semicolonfull 
   ;
|  VirtualSemicolon

Empty Statement

EmptyStatementabbrev 
   ;
|  «empty»
EmptyStatementabbrevNonEmpty  ;
EmptyStatementabbrevNoShortIf  ;
EmptyStatementfull  ;

Expression Statement

ExpressionStatement  [lookahead{function{}] ExpressionallowIn

Block

AnnotatedBlock 
   Block
|  Visibility [no line break] Block
Block  { TopStatements }
TopStatements 
   TopStatementabbrev
|  TopStatementsPrefix TopStatementabbrevNonEmpty
TopStatementsPrefix 
   TopStatementfull
|  TopStatementsPrefix TopStatementfull

Boxes

A box has the syntax:

   box { Statement ... Statement }

A box behaves like a regular block except that it forms its own scope. Variable and function definitions without a Visibility prefix inside the box belong to that box instead of the global scope or the enclosing function, class, or box.

Visibility-Specifying Blocks

A block can be annotated with a Visibility prefix as follows:

   Visibility { Statement ... Statement }

Such a block behaves like a regular block except that every declaration inside that block (but not inside any enclosed function, class, box, or nested visibility-specifying block) that does not have an explicit Visibility prefix uses the Visibility prefix given by the block.

Visibility-specifying blocks are useful to define several items without having to repeat a Visibility prefix for each one. For example,

class foo {
  field z:integer;
  public field a;
  private field b;
  public method f() {}
  public method g(x:integer) {}
}

is equivalent to:

class foo {
  field z:integer;
  public {
    field a;
    private field b;
    method f() {}
    method g(x:integer) {}
  }
}

Labeled Statements

LabeledStatementw  Identifier : Statementw

If Statement

IfStatementabbrev 
   if ParenthesizedExpression Statementabbrev
|  if ParenthesizedExpression StatementabbrevNoShortIf else Statementabbrev
IfStatementabbrevNonEmpty 
   if ParenthesizedExpression StatementabbrevNonEmpty
|  if ParenthesizedExpression StatementabbrevNoShortIf else StatementabbrevNonEmpty
IfStatementfull 
   if ParenthesizedExpression Statementfull
|  if ParenthesizedExpression StatementabbrevNoShortIf else Statementfull
IfStatementabbrevNoShortIf  if ParenthesizedExpression StatementabbrevNoShortIf else StatementabbrevNoShortIf

The semicolon is optional before the else.

Switch Statement

SwitchStatement 
   switch ParenthesizedExpression { }
|  switch ParenthesizedExpression { CaseGroups LastCaseGroup }
CaseGroups 
   «empty»
|  CaseGroups CaseGroup
CaseGroup  CaseGuards CaseStatementsPrefix
LastCaseGroup  CaseGuards CaseStatements
CaseGuards 
   CaseGuard
|  CaseGuards CaseGuard
CaseGuard 
   case ExpressionallowIn :
|  default :
CaseStatements 
   Statementabbrev
|  CaseStatementsPrefix StatementabbrevNonEmpty
CaseStatementsPrefix 
   Statementfull
|  CaseStatementsPrefix Statementfull

Do-While Statement

DoStatement  do StatementabbrevNonEmpty while ParenthesizedExpression

The semicolon is optional before the closing while.

While Statement

WhileStatementw  while ParenthesizedExpression Statementw

For Statements

ForStatementw 
   for ( ForInitializer ; OptionalExpression ; OptionalExpression ) Statementw
|  for ( ForInBinding in ExpressionallowIn ) Statementw
ForInitializer 
   «empty»
|  ExpressionnoIn
|  VariableDefinitionKind VariableBindingListnoIn
ForInBinding 
   PostfixExpression
|  VariableDefinitionKind VariableBindingnoIn

With Statement

WithStatementw  with ParenthesizedExpression Statementw

Continue and Break Statements

ContinueStatement  continue [no line break] OptionalLabel
BreakStatement  break [no line break] OptionalLabel
OptionalLabel 
   «empty»
|  Identifier

Return Statement

ReturnStatement  return [no line break] OptionalExpression

Throw Statement

ThrowStatement  throw [no line break] ExpressionallowIn

Try Statement

TryStatement 
   try AnnotatedBlock CatchClauses
|  try AnnotatedBlock FinallyClause
|  try AnnotatedBlock CatchClauses FinallyClause
CatchClauses 
   CatchClause
|  CatchClauses CatchClause
CatchClause  catch ( TypedIdentifierallowIn ) AnnotatedBlock
FinallyClause  finally AnnotatedBlock

Programs

Program  TopStatements

Waldemar Horwat
Last modified Thursday, November 11, 1999
previousupnext