ECMAScript 4 Netscape Proposal
Core Language
Expressions
|
Monday, June 30, 2003
Most of the behavior of expressions is the same as in ECMAScript 3. Differences are highlighted below.
The above keywords are not reserved and may be used in identifiers.
Just like in ECMAScript Edition 3, an identifier evaluates to an internal data structure called a reference. However, ECMAScript
4 references can be qualified by a qualifier, which, in the general syntax, is a ParenExpression
that evaluates to a namespace. For convenience, if the ParenExpression
consists of a single identifier, the parentheses may be omitted: (a)::m
may be written as a::m
.
The reserved words public
and private
may also be used as qualifiers. public
evaluates
to the public
namespace. internal
(which is not a reserved
word) evaluates to the containing package’s anonymous namespace. private
can only be used inside a class and evaluates to the containing class’s anonymous namespace.
See the name lookup section for more information on the ::
operator.
null
true
false
this
public
evaluates to the public
namespace. private
can be used only inside a class and evaluates to that class’s private namespace.
this
may only be used in methods, constructors,
or functions with the prototype
attribute set.
A FunctionExpression creates and returns an anonymous function.
super
, which may only be used inside a class C, can be applied to a subexpression that evaluates
to an instance v of C. That subexpression can be either a ParenExpression
or omitted, in which case it defaults to this
.
As specified in the grammar below, the SuperExpression must be embedded
as the left operand of a .
(property lookup) or []
(indexing) operator. super
changes
the behavior of the operator in which it is embedded by limiting its property search to definitions inherited from class C’s
superclass. See property lookup.
++
--
A SimpleQualifiedIdentifier or ExpressionQualifiedIdentifier expression id resolves to the binding of id in the innermost enclosing scope that has a visible binding of id. If a qualifier q is 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 namespace q.
The .
operator accepts a QualifiedIdentifier as the
second operand and performs a property lookup.
The grammar allows the []
operator to take multiple arguments. However, all
built-in objects take at most one argument. Implementation-defined host objects may take more arguments.
For most objects other than arrays and some host objects, the expression o[
m]
explicitly coerces m to a qualified name q and
returns the result of o.
q. See property lookup.
An argument list may contain a final argument preceded by ...
. That argument must be an Array
and cannot be null
. The elements of that array become additional arguments to the function, following the arguments
preceding the ...
, if any; the array itself is not passed as an argument. The array must not contain holes.
delete
PostfixExpressionvoid
UnaryExpressiontypeof
UnaryExpression++
PostfixExpression--
PostfixExpression+
UnaryExpression-
UnaryExpression-
NegatedMinLong~
UnaryExpression!
UnaryExpressionThe typeof
operator returns a string as in ECMAScript 3. There is no way to query the
most specific class of an object — all one can ask is whether an object is a member of a specific class.
The expression a is
b takes an expression that must evaluate
to a type as its second operand b. When a is not null
, the expression a is
b
returns true
if a is a member of type b and false
otherwise; this is equivalent to testing whether a can be stored in a variable of type b without coercion.
When a is null
, a is
b behaves analogously to method
dispatch and returns true
if b is either Object
or Null
and false
otherwise. As a special case, when a is –0.0 and b is sbyte
, byte
, short
,
ushort
, int
, or uint
, a is
b returns true
.
The expression a as
b returns a if a
is a member of type b. Otherwise, if a can be implicitly
coerced to type b, then the result is the result of that implicit coercion. Otherwise, a as
b
returns null
if null
is a member of type b or throws an exception otherwise. In any case
b must evaluate to a type.
The instanceof
operator behaves in the same way as in ECMAScript 3 — a instanceof
b
follows a’s prototype chain.
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.
A compile-time constant expression is an expression that either produces an error or evaluates to a value that can be determined at compile time.
The reason that a compile-time constant expression is not guaranteed to always evaluate successfully at run
time is that global name lookup cannot be guaranteed to succeed. It is possible for a program to import a package P
that defines a global constant P::
A that can be accessed as A and then dynamically
define another top-level variable Q::
A that collides with A. It does not appear
to be practical to restrict compile-time constant expressions to only qualified names to eliminate the possibility of such
collisions.
A compile-time expression can consist of the following:
null
, numeric, boolean, and string constants.+
(unary and binary), -
(unary and binary), ~
, !
,
*
, /
, %
, <<
, >>
, >>>
,
<
, >
, <=
, >=
, is
, as
, in
,
instanceof
, ==
, !=
, ===
, !==
, &
, ^
,
|
, &&
, ^^
, ||
, ?:
, 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. An ECMAScript 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 definition D of a compile-time constant 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
directive. If D is visible to R
by virtue of an intervening use
directive U, then the conditions below have to be satisfied both with
respect to D and R and with respect to U and R.
use
directive between
D and R.Some compile-time constant expressions only allow references to definitions that are textually prior to the point of the reference. Other compile-time constant expressions allow forward references to later compile-time constant definitions.
ECMAScript 4 imposes the following restrictions:
const
definitions defining constants that are used in compile-time constant expressions must have initializers
that are themselves compile-time constant expressions. These initializers cannot contain forward references.import
, class
,
and namespace
directive 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
definition.A statement A dominates statement B if any of the following conditions are met:
case
or default
labels between them.false
, and C dominates B.Note that the above definition is conservative. If statement A dominates statement B, then it is guaranteed that, if B is executed then A must have been executed earlier; however, there may be some other statements A' that also are guaranteed to have been executed before B but which do not dominate B by the above definition.
A statement A is dead if any of the following conditions are met:
false
.break
, continue
, return
, or throw
statement B
such that statements A and B are in the same block with B before A and no
case
or default
labels between them.Note that the above definition is conservative. If a statement is dead, then it is guaranteed that it cannot be executed; however, there may be statements that cannot be executed that are not dead by the above definition.
A statement is live if it is not dead.
Waldemar Horwat Last modified Monday, June 30, 2003 |