July 2000 Draft
JavaScript 2.0
Core Language
Expressions
|
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.
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.
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.
null
true
false
public
this
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.
A FunctionExpression creates and returns an anonymous function.
++
--
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.
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.
delete
PostfixExpressiontypeof
UnaryExpressioneval
UnaryExpression++
PostfixExpression--
PostfixExpression+
UnaryExpression-
UnaryExpression~
UnaryExpression!
UnaryExpressionThe ^^
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.
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:
null
, numeric, boolean, and string constants.+
(unary and binary), -
(unary and binary), ~
, !
,
*
, /
, %
, <<
, >>
, >>>
,
<
, >
, <=
, >=
, instanceof
, in
,
==
, !=
, ===
, !==
, &
, ^
, |
,
&&
, ^^
, ||
, ?:
, and ,
as long as they are used
only on numbers, booleans, strings, null
, or undefined
.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.
use
statement between
D and R.A statement A dominates statement B if any of the following conditions are met:
case
or default
labels between them.if
statement C, the condition of
C is a compile-time constant expression whose value is true, and C dominates B.if
statement C, the condition of
C is a compile-time constant expression whose value is false, and C dominates B.In order to make JavaScript 2.0 easier to compile, the following restrictions are imposed:
import
, class
, interface
, and namespace
statement must dominate
the end of the program or package. This restriction limits these statements to the top level of the program, a top-level
block, or a top-level conditional whose condition is known at compile time.class
or interface
statement.
Waldemar Horwat Last modified Tuesday, June 6, 2000 |