This is the spec text of the Optional Chaining proposal in ECMAScript.
For the syntax, we use the ?.
token, with a lookahead at the level of the lexical grammar that allows to discriminate between a?.b
(optional chaining) and a?.3:0
(conditional operator, whose meaning cannot be changed due to backward compatibility constraints).
An early version of this proposal used a Nil reference to express short-circuiting. This one is based on syntax only.
Normative additions are marked like this. In order to avoid distraction, we may omit mere editorial amendments.
The following features may not be evident at a cursory read:
a?.b = c
. This is handled by defining properly the IsSimpleAssignmentTarget delete a ?.b
is supported. There is no change needed in Section When processing an instance of the production
This production exists in order to prevent automatic semicolon insertion rules (
a?.b
`c`
so that it would be interpreted as two valid statements. The purpose is to maintain consistency with similar code without optional chaining operator:
a.b
`c`
which is a valid statement and where automatic semicolon insertion does not apply.
With parameter symbol.
super
, return Properties are accessed by name, using either the dot notation:
or the bracket notation:
The dot notation is explained by the following syntactic conversion:
is identical in its behaviour to
and similarly
is identical in its behaviour to
where <identifier-name-string> is the result of evaluating StringValue of
Is evaluated in exactly the same manner as
The abstract operation EvaluateDynamicPropertyAccess takes as arguments a value baseValue, a
The abstract operation EvaluateStaticPropertyAccess takes as arguments a value baseValue, a
"eval"
, then"eval"
, thenA
?.
.With parameters baseValue and baseReference.
With parameter call.