July 2000 Draft
JavaScript 2.0
Core Language
Expressions
previousupnext

Tuesday, June 6, 2000

Most of the behavior of expressions is the same as in JavaScript 1.5. Differences are highlighted below. One general difference is that most expression operators can be overridden via operator overloading.

  {allowInnoIn}

Identifiers

AttributeIdentifier 
   Identifier
|  get
|  set
Identifier 
   AttributeIdentifier
|  attribute
|  constructor
|  language
|  namespace
|  use

The above keywords are not reserved and may be used in identifiers. get and set may be used as user-defined declaration attributes; the other keywords above may not be used as declaration attributes.

QualifiedIdentifier 
   Identifier
|  Qualifier :: QualifiedIdentifier
Qualifier 
   Identifier
|  public
|  package
|  private
|  super
|  ParenthesizedExpression

Just like in ECMAScript Edition 3, an identifier evaluates to an internal data structure called a reference. However, JavaScript 2.0 references can be qualified by zero or more qualifiers, which are expressions that must evaluate to either namespaces, classes, interfaces, or packages. A qualifier is set to the value of the ParenthesizedExpression. If the ParenthesizedExpression is a simple Identifier then the parentheses may be omitted.

The reserved words public, package, private, and super may also be used as qualifiers. public evaluates to the public namespace. package evaluates to the containing package's anonymous namespace. If used in a class, private evaluates to the containing class's anonymous namespace; otherwise private is the same as package. super, which may only be used inside a class, restricts the interpretation of the Identifier to definitions inherited from the superclass.

Primary Expressions

PrimaryExpression 
   null
|  true
|  false
|  public
|  Number
|  Number [no line break] String
|  String
|  this
|  QualifiedIdentifier
|  RegularExpression
|  ParenthesizedExpression
|  ParenthesizedExpression [no line break] String
|  ArrayLiteral
|  ObjectLiteral
|  FunctionExpression
ParenthesizedExpression  ( ExpressionallowIn )

A Number literal or ParenthesizedExpression followed by a String literal is a unit expression. The unit object specified by the String is looked up; the result is called as a function and passed two arguments: the numeric value of the Number literal or ParenthesizedExpression, and either null (if a ParenthesizedExpression was provided) or the original Number literal expressed as a string.

The string representation allows user-defined unit classes to define extended syntaxes for numbers. For instance, a long-integer package might define a unit called "L" that treats the Number literal as a full 64-bit number without rounding it to a double first.

public evaluates to the public namespace.

this may only be used in methods or traditional functions.

A QualifiedIdentifier expression id resolves to the binding of id in the innermost enclosing scope that has a visible binding of id. If qualifiers are present before the id, then the QualifiedIdentifier expression resolves to the binding of id in the innermost enclosing scope that has a visible binding of id in the namespaces, classes, interfaces, and/or packages specified by the qualifiers.

Function Expressions

FunctionExpression 
   function FunctionSignature Block
|  function Identifier FunctionSignature Block

A FunctionExpression creates and returns an anonymous function.

Object Literals

ObjectLiteral 
   { }
|  { FieldList }
FieldList 
   LiteralField
|  FieldList , LiteralField
LiteralField  FieldName : AssignmentExpressionallowIn
FieldName 
   QualifiedIdentifier
|  String
|  Number

Array Literals

ArrayLiteral  [ ElementList ]
ElementList 
   LiteralElement
|  ElementList , LiteralElement
LiteralElement 
   «empty»
|  AssignmentExpressionallowIn

Postfix Unary Operators

PostfixExpression 
   FullPostfixExpression
|  ShortNewExpression
FullPostfixExpression 
   PrimaryExpression
|  FullNewExpression
|  FullPostfixExpression MemberOperator
|  FullPostfixExpression Arguments
|  PostfixExpression [no line break] ++
|  PostfixExpression [no line break] --
FullNewExpression  new FullNewSubexpression Arguments
ShortNewExpression  new ShortNewSubexpression
FullNewSubexpression 
   PrimaryExpression
|  FullNewSubexpression MemberOperator
|  FullNewExpression
ShortNewSubexpression 
   FullNewSubexpression
|  ShortNewExpression
MemberOperator 
   [ ArgumentList ]
|  . QualifiedIdentifier
|  . ParenthesizedExpression
|  @ QualifiedIdentifier
|  @ ParenthesizedExpression

The @ operator performs a type coercion. The second operand specifies the type. Both the . and the @ operators accept either a QualifiedIdentifier or a ParenthesizedExpression as the second operand. If it is a ParenthesizedExpression, the second operand of . must evaluate to a string. a.(x) is a synonym for a[x] except that the latter can be overridden via operator overloading.

The [] operator can take multiple (or even named) arguments. This allows users to define data structures such as multidimensional arrays via operator overloading.

Unless the [] operator is overridden for an object o, the expression o[m] coerces m to a string s and returns the result of o.s, limiting the name lookup to the indexable members of o.

Arguments  ( ArgumentList )
ArgumentList 
   «empty»
|  ArgumentListPrefix
|  NamedArgumentListPrefix
ArgumentListPrefix 
   AssignmentExpressionallowIn
|  ArgumentListPrefix , AssignmentExpressionallowIn
NamedArgumentListPrefix 
   LiteralField
|  ArgumentListPrefix , LiteralField
|  NamedArgumentListPrefix , LiteralField

An ArgumentList can contain both positional and named arguments. Named arguments use the same syntax as object literals. Named arguments (and any subsequent unnamed arguments) can only match a function's rest parameter.

Prefix Unary Operators

UnaryExpression 
   PostfixExpression
|  delete PostfixExpression
|  typeof UnaryExpression
|  eval UnaryExpression
|  ++ PostfixExpression
|  -- PostfixExpression
|  + UnaryExpression
|  - UnaryExpression
|  ~ UnaryExpression
|  ! UnaryExpression

Multiplicative Operators

MultiplicativeExpression 
   UnaryExpression
|  MultiplicativeExpression * UnaryExpression
|  MultiplicativeExpression / UnaryExpression
|  MultiplicativeExpression % UnaryExpression

Additive Operators

AdditiveExpression 
   MultiplicativeExpression
|  AdditiveExpression + MultiplicativeExpression
|  AdditiveExpression - MultiplicativeExpression

Bitwise Shift Operators

ShiftExpression 
   AdditiveExpression
|  ShiftExpression << AdditiveExpression
|  ShiftExpression >> AdditiveExpression
|  ShiftExpression >>> AdditiveExpression

Relational Operators

RelationalExpressionallowIn 
   ShiftExpression
|  RelationalExpressionallowIn < ShiftExpression
|  RelationalExpressionallowIn > ShiftExpression
|  RelationalExpressionallowIn <= ShiftExpression
|  RelationalExpressionallowIn >= ShiftExpression
|  RelationalExpressionallowIn instanceof ShiftExpression
|  RelationalExpressionallowIn in ShiftExpression
RelationalExpressionnoIn 
   ShiftExpression
|  RelationalExpressionnoIn < ShiftExpression
|  RelationalExpressionnoIn > ShiftExpression
|  RelationalExpressionnoIn <= ShiftExpression
|  RelationalExpressionnoIn >= ShiftExpression
|  RelationalExpressionnoIn instanceof ShiftExpression

Equality Operators

EqualityExpression 
   RelationalExpression
|  EqualityExpression == RelationalExpression
|  EqualityExpression != RelationalExpression
|  EqualityExpression === RelationalExpression
|  EqualityExpression !== RelationalExpression

Binary Bitwise Operators

BitwiseAndExpression 
   EqualityExpression
|  BitwiseAndExpression & EqualityExpression
BitwiseXorExpression 
   BitwiseAndExpression
|  BitwiseXorExpression ^ BitwiseAndExpression
BitwiseOrExpression 
   BitwiseXorExpression
|  BitwiseOrExpression | BitwiseXorExpression

Binary Logical Operators

LogicalAndExpression 
   BitwiseOrExpression
|  LogicalAndExpression && BitwiseOrExpression
LogicalXorExpression 
   LogicalAndExpression
|  LogicalXorExpression ^^ LogicalAndExpression

The ^^ operator is a logical exclusive-or operator. It evaluates both operands. If they both convert to true or both convert to false, then ^^ returns false; otherwise ^^ returns the unconverted value of whichever argument converted to true.

LogicalOrExpression 
   LogicalXorExpression
|  LogicalOrExpression || LogicalXorExpression

Conditional Operator

ConditionalExpression 
   LogicalOrExpression
|  LogicalOrExpression ? AssignmentExpression : AssignmentExpression
NonAssignmentExpression 
   LogicalOrExpression
|  LogicalOrExpression ? NonAssignmentExpression : NonAssignmentExpression

Assignment Operators

AssignmentExpression 
   ConditionalExpression
|  PostfixExpression = AssignmentExpression
|  PostfixExpression CompoundAssignment AssignmentExpression
CompoundAssignment 
   *=
|  /=
|  %=
|  +=
|  -=
|  <<=
|  >>=
|  >>>=
|  &=
|  ^=
|  |=
|  &&=
|  ^^=
|  ||=

Expressions

Expression 
   AssignmentExpression
|  Expression , AssignmentExpression
OptionalExpression 
   ExpressionallowIn
|  «empty»

Type Expressions

TypeExpression  NonAssignmentExpression

Compile-Time Constant Expressions

The value of a compile-time constant expression can be determined at compile time. If the expression were evaluated at run time, it would be guaranteed to evaluate to the same value.

A compile-time expression can consist of the following:

A pure function cannot have any read or write side effects or create any objects. A pure function's result depends only on its arguments. A JavaScript host embedding may define some pure functions. Currently there is no way for a script to define any such functions, but a future language extension may permit that.

A reference R to a previous definition D of a constant, function, class, interface, or namespace is allowed inside a compile-time constant expression as long as the conditions below are met. If D was imported from another package, then the location of D is considered to be the location of the import statement. If D is visible to R by virtue of an intervening use statement U, then the conditions below have to be satisfied both with respect to D and R and with respect to U and R.

A statement A dominates statement B if any of the following conditions are met:

Restrictions

In order to make JavaScript 2.0 easier to compile, the following restrictions are imposed:


Waldemar Horwat
Last modified Tuesday, June 6, 2000
previousupnext