ECMAScript 4 Netscape Proposal
Core Language
Statements
previousupnext

Wednesday, June 4, 2003

Statements

Most of the behavior of statements is the same as in ECMAScript 3. Differences are highlighted below.

  {abbrevnoShortIffull}
Statement 
   ExpressionStatement Semicolon
|  SuperStatement Semicolon
|  Block
|  LabeledStatement
|  IfStatement
|  SwitchStatement
|  DoStatement Semicolon
|  WhileStatement
|  ForStatement
|  WithStatement
|  ContinueStatement Semicolon
|  BreakStatement Semicolon
|  ReturnStatement Semicolon
|  ThrowStatement Semicolon
|  TryStatement
Substatement 
   EmptyStatement
|  Statement
|  SimpleVariableDefinition Semicolon
|  Attributes [no line break] { Substatements }
Substatements 
   «empty»
|  SubstatementsPrefix Substatementabbrev
SubstatementsPrefix 
   «empty»
|  SubstatementsPrefix Substatementfull
Semicolonabbrev 
   ;
|  VirtualSemicolon
|  «empty»
SemicolonnoShortIf 
   ;
|  VirtualSemicolon
|  «empty»
Semicolonfull 
   ;
|  VirtualSemicolon

A Substatement is a statement directly contained by one of the compound statements label:, if, switch, while, do while, for, or with (but not a block). A substatement cannot be a directive except that, in non-strict mode only, it can be a var definition without attributes or types.

A substatement can also consist of one or more attributes applied to a group of substatements enclosed in braces. The attributes must evaluate to either true or false. The braces do not form a scope in this case.

The Semicolon productions allow both grammatical and line-break semicolon insertion.

Empty Statement

EmptyStatement  ;

Expression Statement

ExpressionStatement  [lookahead{function{}] ListExpressionallowIn

Super Statement

SuperStatement  super Arguments

The super statement calls the superclass’s constructor. It can only be used inside a class’s constructor.

Block Statement

Block  { Directives }

A block groups statements and forms a scope.

Labeled Statements

LabeledStatement  Identifier : Substatement

If Statement

IfStatementabbrev 
   if ParenListExpression Substatementabbrev
|  if ParenListExpression SubstatementnoShortIf else Substatementabbrev
IfStatementfull 
   if ParenListExpression Substatementfull
|  if ParenListExpression SubstatementnoShortIf else Substatementfull
IfStatementnoShortIf  if ParenListExpression SubstatementnoShortIf else SubstatementnoShortIf

The semicolon is optional before the else.

Switch Statement

SwitchStatement  switch ParenListExpression { CaseElements }
CaseElements 
   «empty»
|  CaseLabel
|  CaseLabel CaseElementsPrefix CaseElementabbrev
CaseElementsPrefix 
   «empty»
|  CaseElementsPrefix CaseElementfull
CaseElement 
   Directive
|  CaseLabel
CaseLabel 
   case ListExpressionallowIn :
|  default :

Do-While Statement

DoStatement  do Substatementabbrev while ParenListExpression

The semicolon is optional before the closing while.

While Statement

WhileStatement  while ParenListExpression Substatement

For Statements

ForStatement 
   for ( ForInitializer ; OptionalExpression ; OptionalExpression ) Substatement
|  for ( ForInBinding in ListExpressionallowIn ) Substatement
ForInitializer 
   «empty»
|  ListExpressionnoIn
|  VariableDefinitionnoIn
|  Attributes [no line break] VariableDefinitionnoIn
ForInBinding 
   PostfixExpression
|  VariableDefinitionKind VariableBindingnoIn
|  Attributes [no line break] VariableDefinitionKind VariableBindingnoIn
OptionalExpression 
   ListExpressionallowIn
|  «empty»

A for statement forms a scope. Any definitions in it (including the ForInitializer and ForInBinding) are visible inside the for statement and its substatement, but not outside the for statement. However, a var definition inside a for statement may be hoisted to the nearest enclosing regional scope.

With Statement

WithStatement  with ParenListExpression Substatement

Continue and Break Statements

ContinueStatement 
   continue
|  continue [no line break] Identifier
BreakStatement 
   break
|  break [no line break] Identifier

Return Statement

ReturnStatement 
   return
|  return [no line break] ListExpressionallowIn

A return statement can only be used inside a function or constructor. The return statement cannot have an expression if used inside a constructor or a setter.

Throw Statement

ThrowStatement  throw [no line break] ListExpressionallowIn

Try Statement

TryStatement 
   try Block CatchClauses
|  try Block CatchClausesOpt finally Block
CatchClausesOpt 
   «empty»
|  CatchClauses
CatchClauses 
   CatchClause
|  CatchClauses CatchClause
CatchClause  catch ( Parameter ) Block

Each CatchClause forms a scope. The Parameter, if any, is defined as a local variable visible only within the CatchClause.

The Blocks following try and finally are also scopes like other Block statements.

Directives

Directive 
   EmptyStatement
|  Statement
|  AnnotatableDirective
|  Attributes [no line break] AnnotatableDirective
|  Attributes [no line break] { Directives }
|  Pragma Semicolon
AnnotatableDirective 
   VariableDefinitionallowIn Semicolon
|  FunctionDefinition
|  ClassDefinition
|  NamespaceDefinition Semicolon
|  ImportDirective Semicolon
|  UseDirective Semicolon
Directives 
   «empty»
|  DirectivesPrefix Directiveabbrev
DirectivesPrefix 
   «empty»
|  DirectivesPrefix Directivefull

Attributes can be applied to a group of directives by following them by a {, the directives, and a }. The attributes apply to all of the enclosed directives. The attribute true is ignored. The attribute false causes all of the enclosed directives to be omitted. When used this way, the braces do not form a block or a scope.

Annotated groups are useful to define several items without having to repeat attributes for each one. For example,

class foo {
  var z:Integer;
  public var a;
  private var b;
  private function f() {}
  private function g(x:Integer):Boolean {}
}

is equivalent to:

class foo {
  var z:Integer;
  public var a;
  private {
    var b;
    function f() {}
    function g(x:Integer):Boolean {}
  }
}

Programs

Program 
   Directives
|  PackageDefinition Program

Waldemar Horwat
Last modified Wednesday, June 4, 2003
previousupnext