14 ECMAScript Language: Statements and Declarations
Syntax
14.1 Statement Semantics
14.1.1 Runtime Semantics: Evaluation
- Return
empty .
- Return ?
Evaluation ofFunctionDeclaration .
- Let newLabelSet be a new empty
List . - Return ?
LabelledEvaluation of thisBreakableStatement with argument newLabelSet.
14.2 Block
Syntax
14.2.1 Static Semantics: Early Errors
-
It is a Syntax Error if the
LexicallyDeclaredNames ofStatementList contains any duplicate entries. -
It is a Syntax Error if any element of the
LexicallyDeclaredNames ofStatementList also occurs in theVarDeclaredNames ofStatementList .
14.2.2 Runtime Semantics: Evaluation
- Return
empty .
- Let oldEnv be the
running execution context 's LexicalEnvironment. - Let blockEnv be
NewDeclarativeEnvironment (oldEnv). - Perform
BlockDeclarationInstantiation (StatementList , blockEnv). - Set the
running execution context 's LexicalEnvironment to blockEnv. - Let blockValue be
Completion (Evaluation ofStatementList ). - Set the
running execution context 's LexicalEnvironment to oldEnv. - Return ? blockValue.
No matter how control leaves the
- Let sl be ?
Evaluation ofStatementList . - Let s be
Completion (Evaluation ofStatementListItem ). - Return ?
UpdateEmpty (s, sl).
The value of a eval function all return the value 1:
eval("1;;;;;")
eval("1;{}")
eval("1;var a;")
14.2.3 BlockDeclarationInstantiation ( code, env )
The abstract operation BlockDeclarationInstantiation takes arguments code (a
When a
It performs the following steps when called:
- Let declarations be the
LexicallyScopedDeclarations of code. - Let privateEnv be the
running execution context 's PrivateEnvironment. - For each element d of declarations, do
- For each element dn of the
BoundNames of d, do- If
IsConstantDeclaration of d istrue , then- Perform ! env.CreateImmutableBinding(dn,
true ).
- Perform ! env.CreateImmutableBinding(dn,
- Else,
- Perform ! env.CreateMutableBinding(dn,
false ). NOTE: This step is replaced in sectionB.3.2.6 .
- Perform ! env.CreateMutableBinding(dn,
- If
- If d is either a
FunctionDeclaration , aGeneratorDeclaration , anAsyncFunctionDeclaration , or anAsyncGeneratorDeclaration , then- Let fn be the sole element of the
BoundNames of d. - Let fo be
InstantiateFunctionObject of d with arguments env and privateEnv. - Perform ! env.InitializeBinding(fn, fo). NOTE: This step is replaced in section
B.3.2.6 .
- Let fn be the sole element of the
- For each element dn of the
- Return
unused .
14.3 Declarations and the Variable Statement
14.3.1 Let and Const Declarations
let and const declarations define variables that are scoped to the let declaration does not have an
Syntax
14.3.1.1 Static Semantics: Early Errors
-
It is a Syntax Error if the
BoundNames ofBindingList contains"let" . -
It is a Syntax Error if the
BoundNames ofBindingList contains any duplicate entries.
-
It is a Syntax Error if
Initializer is not present andIsConstantDeclaration of theLexicalDeclaration containing thisLexicalBinding istrue .
14.3.1.2 Runtime Semantics: Evaluation
- Perform ?
Evaluation ofBindingList . - Return
empty .
- Perform ?
Evaluation ofBindingList . - Return ?
Evaluation ofLexicalBinding .
- Let lhs be !
ResolveBinding (StringValue ofBindingIdentifier ). - Perform !
InitializeReferencedBinding (lhs,undefined ). - Return
empty .
A const declaration.
- Let bindingId be the
StringValue ofBindingIdentifier . - Let lhs be !
ResolveBinding (bindingId). - If
IsAnonymousFunctionDefinition (Initializer ) istrue , then- Let value be ?
NamedEvaluation ofInitializer with argument bindingId.
- Let value be ?
- Else,
- Let rhs be ?
Evaluation ofInitializer . - Let value be ?
GetValue (rhs).
- Let rhs be ?
- Perform !
InitializeReferencedBinding (lhs, value). - Return
empty .
- Let rhs be ?
Evaluation ofInitializer . - Let value be ?
GetValue (rhs). - Let env be the
running execution context 's LexicalEnvironment. - Return ?
BindingInitialization ofBindingPattern with arguments value and env.
14.3.2 Variable Statement
A var statement declares variables that are scoped to the
Syntax
14.3.2.1 Runtime Semantics: Evaluation
- Perform ?
Evaluation ofVariableDeclarationList . - Return
empty .
- Perform ?
Evaluation ofVariableDeclarationList . - Return ?
Evaluation ofVariableDeclaration .
- Return
empty .
- Let bindingId be the
StringValue ofBindingIdentifier . - Let lhs be ?
ResolveBinding (bindingId). - If
IsAnonymousFunctionDefinition (Initializer ) istrue , then- Let value be ?
NamedEvaluation ofInitializer with argument bindingId.
- Let value be ?
- Else,
- Let rhs be ?
Evaluation ofInitializer . - Let value be ?
GetValue (rhs).
- Let rhs be ?
- Perform ?
PutValue (lhs, value). - Return
empty .
If a
- Let rhs be ?
Evaluation ofInitializer . - Let rVal be ?
GetValue (rhs). - Return ?
BindingInitialization ofBindingPattern with arguments rVal andundefined .
14.3.3 Destructuring Binding Patterns
Syntax
14.3.3.1 Runtime Semantics: PropertyBindingInitialization
The
- Let boundNames be ? PropertyBindingInitialization of
BindingPropertyList with arguments value and environment. - Let nextNames be ? PropertyBindingInitialization of
BindingProperty with arguments value and environment. - Return the
list-concatenation of boundNames and nextNames.
- Let name be the sole element of the
BoundNames ofSingleNameBinding . - Perform ?
KeyedBindingInitialization ofSingleNameBinding with arguments value, environment, and name. - Return « name ».
- Let P be ?
Evaluation ofPropertyName . - Perform ?
KeyedBindingInitialization ofBindingElement with arguments value, environment, and P. - Return « P ».
14.3.3.2 Runtime Semantics: RestBindingInitialization
The
- Let lhs be ?
ResolveBinding (StringValue ofBindingIdentifier , environment). - Let restObj be
OrdinaryObjectCreate (%Object.prototype% ). - Perform ?
CopyDataProperties (restObj, value, excludedNames). - If environment is
undefined , return ?PutValue (lhs, restObj). - Return ?
InitializeReferencedBinding (lhs, restObj).
14.3.3.3 Runtime Semantics: KeyedBindingInitialization
The
When
It is defined piecewise over the following productions:
- Let v be ?
GetV (value, propertyName). - If
Initializer is present and v isundefined , then- Let defaultValue be ?
Evaluation ofInitializer . - Set v to ?
GetValue (defaultValue).
- Let defaultValue be ?
- Return ?
BindingInitialization ofBindingPattern with arguments v and environment.
- Let bindingId be the
StringValue ofBindingIdentifier . - Let lhs be ?
ResolveBinding (bindingId, environment). - Let v be ?
GetV (value, propertyName). - If
Initializer is present and v isundefined , then- If
IsAnonymousFunctionDefinition (Initializer ) istrue , then- Set v to ?
NamedEvaluation ofInitializer with argument bindingId.
- Set v to ?
- Else,
- Let defaultValue be ?
Evaluation ofInitializer . - Set v to ?
GetValue (defaultValue).
- Let defaultValue be ?
- If
- If environment is
undefined , return ?PutValue (lhs, v). - Return ?
InitializeReferencedBinding (lhs, v).
14.4 Empty Statement
Syntax
14.4.1 Runtime Semantics: Evaluation
- Return
empty .
14.5 Expression Statement
Syntax
An function or class async function because that would make it ambiguous with an let [ because that would make it ambiguous with a let
14.5.1 Runtime Semantics: Evaluation
- Let exprRef be ?
Evaluation ofExpression . - Return ?
GetValue (exprRef).
14.6 The if Statement
Syntax
else] resolves the classic "dangling else" problem in the usual way. That is, when the choice of associated if is otherwise ambiguous, the else is associated with the nearest (innermost) of the candidate ifs14.6.1 Static Semantics: Early Errors
-
It is a Syntax Error if
IsLabelledFunction (the firstStatement ) istrue . -
It is a Syntax Error if
IsLabelledFunction (the secondStatement ) istrue .
-
It is a Syntax Error if
IsLabelledFunction (Statement ) istrue .
It is only necessary to apply this rule if the extension specified in
14.6.2 Runtime Semantics: Evaluation
- Let exprRef be ?
Evaluation ofExpression . - Let exprValue be
ToBoolean (?GetValue (exprRef)). - If exprValue is
true , then- Let stmtCompletion be
Completion (Evaluation of the firstStatement ).
- Let stmtCompletion be
- Else,
- Let stmtCompletion be
Completion (Evaluation of the secondStatement ).
- Let stmtCompletion be
- Return ?
UpdateEmpty (stmtCompletion,undefined ).
- Let exprRef be ?
Evaluation ofExpression . - Let exprValue be
ToBoolean (?GetValue (exprRef)). - If exprValue is
false , then- Return
undefined .
- Return
- Else,
- Let stmtCompletion be
Completion (Evaluation ofStatement ). - Return ?
UpdateEmpty (stmtCompletion,undefined ).
- Let stmtCompletion be
14.7 Iteration Statements
Syntax
14.7.1 Semantics
14.7.1.1 LoopContinues ( completion, labelSet )
The abstract operation LoopContinues takes arguments completion (a
- If completion is a
normal completion , returntrue . - If completion is not a
continue completion , returnfalse . - If completion.[[Target]] is
empty , returntrue . - If labelSet contains completion.[[Target]], return
true . - Return
false .
Within the
14.7.1.2 Runtime Semantics: LoopEvaluation
The
- Return ?
DoWhileLoopEvaluation ofDoWhileStatement with argument labelSet.
- Return ?
WhileLoopEvaluation ofWhileStatement with argument labelSet.
- Return ?
ForLoopEvaluation ofForStatement with argument labelSet.
- Return ?
ForInOfLoopEvaluation ofForInOfStatement with argument labelSet.
14.7.2 The do-while Statement
Syntax
14.7.2.1 Static Semantics: Early Errors
-
It is a Syntax Error if
IsLabelledFunction (Statement ) istrue .
It is only necessary to apply this rule if the extension specified in
14.7.2.2 Runtime Semantics: DoWhileLoopEvaluation
The
- Let V be
undefined . - Repeat,
- Let stmtResult be
Completion (Evaluation ofStatement ). - If
LoopContinues (stmtResult, labelSet) isfalse , return ?UpdateEmpty (stmtResult, V). - If stmtResult.[[Value]] is not
empty , set V to stmtResult.[[Value]]. - Let exprRef be ?
Evaluation ofExpression . - Let exprValue be ?
GetValue (exprRef). - If
ToBoolean (exprValue) isfalse , return V.
- Let stmtResult be
14.7.3 The while Statement
Syntax
14.7.3.1 Static Semantics: Early Errors
-
It is a Syntax Error if
IsLabelledFunction (Statement ) istrue .
It is only necessary to apply this rule if the extension specified in
14.7.3.2 Runtime Semantics: WhileLoopEvaluation
The
- Let V be
undefined . - Repeat,
- Let exprRef be ?
Evaluation ofExpression . - Let exprValue be ?
GetValue (exprRef). - If
ToBoolean (exprValue) isfalse , return V. - Let stmtResult be
Completion (Evaluation ofStatement ). - If
LoopContinues (stmtResult, labelSet) isfalse , return ?UpdateEmpty (stmtResult, V). - If stmtResult.[[Value]] is not
empty , set V to stmtResult.[[Value]].
- Let exprRef be ?
14.7.4 The for Statement
Syntax
14.7.4.1 Static Semantics: Early Errors
-
It is a Syntax Error if
IsLabelledFunction (Statement ) istrue .
It is only necessary to apply this rule if the extension specified in
-
It is a Syntax Error if any element of the
BoundNames ofLexicalDeclaration also occurs in theVarDeclaredNames ofStatement .
14.7.4.2 Runtime Semantics: ForLoopEvaluation
The
- If the first
Expression is present, then- Let exprRef be ?
Evaluation of the firstExpression . - Perform ?
GetValue (exprRef).
- Let exprRef be ?
- If the second
Expression is present, let test be the secondExpression ; otherwise, let test beempty . - If the third
Expression is present, let increment be the thirdExpression ; otherwise, let increment beempty . - Return ?
ForBodyEvaluation (test, increment,Statement , « », labelSet).
- Perform ?
Evaluation ofVariableDeclarationList . - If the first
Expression is present, let test be the firstExpression ; otherwise, let test beempty . - If the second
Expression is present, let increment be the secondExpression ; otherwise, let increment beempty . - Return ?
ForBodyEvaluation (test, increment,Statement , « », labelSet).
- Let oldEnv be the
running execution context 's LexicalEnvironment. - Let loopEnv be
NewDeclarativeEnvironment (oldEnv). - Let isConst be
IsConstantDeclaration ofLexicalDeclaration . - Let boundNames be the
BoundNames ofLexicalDeclaration . - For each element dn of boundNames, do
- If isConst is
true , then- Perform ! loopEnv.CreateImmutableBinding(dn,
true ).
- Perform ! loopEnv.CreateImmutableBinding(dn,
- Else,
- Perform ! loopEnv.CreateMutableBinding(dn,
false ).
- Perform ! loopEnv.CreateMutableBinding(dn,
- If isConst is
- Set the
running execution context 's LexicalEnvironment to loopEnv. - Let forDcl be
Completion (Evaluation ofLexicalDeclaration ). - If forDcl is an
abrupt completion , then- Set the
running execution context 's LexicalEnvironment to oldEnv. - Return ? forDcl.
- Set the
- If isConst is
false , let perIterationLets be boundNames; otherwise let perIterationLets be a new emptyList . - If the first
Expression is present, let test be the firstExpression ; otherwise, let test beempty . - If the second
Expression is present, let increment be the secondExpression ; otherwise, let increment beempty . - Let bodyResult be
Completion (ForBodyEvaluation (test, increment,Statement , perIterationLets, labelSet)). - Set the
running execution context 's LexicalEnvironment to oldEnv. - Return ? bodyResult.
14.7.4.3 ForBodyEvaluation ( test, increment, stmt, perIterationBindings, labelSet )
The abstract operation ForBodyEvaluation takes arguments test (an
- Let V be
undefined . - Perform ?
CreatePerIterationEnvironment (perIterationBindings). - Repeat,
- If test is not
empty , then- Let testRef be ?
Evaluation of test. - Let testValue be ?
GetValue (testRef). - If
ToBoolean (testValue) isfalse , return V.
- Let testRef be ?
- Let result be
Completion (Evaluation of stmt). - If
LoopContinues (result, labelSet) isfalse , return ?UpdateEmpty (result, V). - If result.[[Value]] is not
empty , set V to result.[[Value]]. - Perform ?
CreatePerIterationEnvironment (perIterationBindings). - If increment is not
empty , then- Let incRef be ?
Evaluation of increment. - Perform ?
GetValue (incRef).
- Let incRef be ?
- If test is not
14.7.4.4 CreatePerIterationEnvironment ( perIterationBindings )
The abstract operation CreatePerIterationEnvironment takes argument perIterationBindings (a
- If perIterationBindings has any elements, then
- Let lastIterationEnv be the
running execution context 's LexicalEnvironment. - Let outer be lastIterationEnv.[[OuterEnv]].
Assert : outer is notnull .- Let thisIterationEnv be
NewDeclarativeEnvironment (outer). - For each element bn of perIterationBindings, do
- Perform ! thisIterationEnv.CreateMutableBinding(bn,
false ). - Let lastValue be ? lastIterationEnv.GetBindingValue(bn,
true ). - Perform ! thisIterationEnv.InitializeBinding(bn, lastValue).
- Perform ! thisIterationEnv.CreateMutableBinding(bn,
- Set the
running execution context 's LexicalEnvironment to thisIterationEnv.
- Let lastIterationEnv be the
- Return
unused .
14.7.5 The for-in, for-of, and for-await-of Statements
Syntax
This section is extended by Annex
14.7.5.1 Static Semantics: Early Errors
-
It is a Syntax Error if
IsLabelledFunction (Statement ) istrue .
It is only necessary to apply this rule if the extension specified in
-
If
LeftHandSideExpression is either anObjectLiteral or anArrayLiteral ,LeftHandSideExpression must cover anAssignmentPattern . -
If
LeftHandSideExpression is neither anObjectLiteral nor anArrayLiteral , it is a Syntax Error if theAssignmentTargetType ofLeftHandSideExpression is notsimple .
-
It is a Syntax Error if the
BoundNames ofForDeclaration contains"let" . -
It is a Syntax Error if any element of the
BoundNames ofForDeclaration also occurs in theVarDeclaredNames ofStatement . -
It is a Syntax Error if the
BoundNames ofForDeclaration contains any duplicate entries.
14.7.5.2 Static Semantics: IsDestructuring
The
- If
PrimaryExpression is either anObjectLiteral or anArrayLiteral , returntrue . - Return
false .
- Return
false .
- Return IsDestructuring of
ForBinding .
- Return
false .
- Return
true .
This section is extended by Annex
14.7.5.3 Runtime Semantics: ForDeclarationBindingInitialization
The
var statements and the formal parameter lists of some
It is defined piecewise over the following productions:
- Return ?
BindingInitialization ofForBinding with arguments value and environment.
14.7.5.4 Runtime Semantics: ForDeclarationBindingInstantiation
The
- For each element name of the
BoundNames ofForBinding , do- If
IsConstantDeclaration ofLetOrConst istrue , then- Perform ! environment.CreateImmutableBinding(name,
true ).
- Perform ! environment.CreateImmutableBinding(name,
- Else,
- Perform ! environment.CreateMutableBinding(name,
false ).
- Perform ! environment.CreateMutableBinding(name,
- If
- Return
unused .
14.7.5.5 Runtime Semantics: ForInOfLoopEvaluation
The
- Let keyResult be ? ForIn/OfHeadEvaluation(« »,
Expression ,enumerate ). - Return ? ForIn/OfBodyEvaluation(
LeftHandSideExpression ,Statement , keyResult,enumerate ,assignment , labelSet).
- Let keyResult be ? ForIn/OfHeadEvaluation(« »,
Expression ,enumerate ). - Return ? ForIn/OfBodyEvaluation(
ForBinding ,Statement , keyResult,enumerate ,var-binding , labelSet).
- Let keyResult be ? ForIn/OfHeadEvaluation(
BoundNames ofForDeclaration ,Expression ,enumerate ). - Return ? ForIn/OfBodyEvaluation(
ForDeclaration ,Statement , keyResult,enumerate ,lexical-binding , labelSet).
- Let keyResult be ? ForIn/OfHeadEvaluation(« »,
AssignmentExpression ,iterate ). - Return ? ForIn/OfBodyEvaluation(
LeftHandSideExpression ,Statement , keyResult,iterate ,assignment , labelSet).
- Let keyResult be ? ForIn/OfHeadEvaluation(« »,
AssignmentExpression ,iterate ). - Return ? ForIn/OfBodyEvaluation(
ForBinding ,Statement , keyResult,iterate ,var-binding , labelSet).
- Let keyResult be ? ForIn/OfHeadEvaluation(
BoundNames ofForDeclaration ,AssignmentExpression ,iterate ). - Return ? ForIn/OfBodyEvaluation(
ForDeclaration ,Statement , keyResult,iterate ,lexical-binding , labelSet).
- Let keyResult be ? ForIn/OfHeadEvaluation(« »,
AssignmentExpression ,async-iterate ). - Return ? ForIn/OfBodyEvaluation(
LeftHandSideExpression ,Statement , keyResult,iterate ,assignment , labelSet,async ).
- Let keyResult be ? ForIn/OfHeadEvaluation(« »,
AssignmentExpression ,async-iterate ). - Return ? ForIn/OfBodyEvaluation(
ForBinding ,Statement , keyResult,iterate ,var-binding , labelSet,async ).
- Let keyResult be ? ForIn/OfHeadEvaluation(
BoundNames ofForDeclaration ,AssignmentExpression ,async-iterate ). - Return ? ForIn/OfBodyEvaluation(
ForDeclaration ,Statement , keyResult,iterate ,lexical-binding , labelSet,async ).
This section is extended by Annex
14.7.5.6 ForIn/OfHeadEvaluation ( uninitializedBoundNames, expr, iterationKind )
The abstract operation ForIn/OfHeadEvaluation takes arguments uninitializedBoundNames (a
- Let oldEnv be the
running execution context 's LexicalEnvironment. - If uninitializedBoundNames is not empty, then
Assert : uninitializedBoundNames has no duplicate entries.- Let newEnv be
NewDeclarativeEnvironment (oldEnv). - For each String name of uninitializedBoundNames, do
- Perform ! newEnv.CreateMutableBinding(name,
false ).
- Perform ! newEnv.CreateMutableBinding(name,
- Set the
running execution context 's LexicalEnvironment to newEnv.
- Let exprRef be
Completion (Evaluation of expr). - Set the
running execution context 's LexicalEnvironment to oldEnv. - Let exprValue be ?
GetValue (? exprRef). - If iterationKind is
enumerate , then- If exprValue is either
undefined ornull , then- Return
Completion Record { [[Type]]:break , [[Value]]:empty , [[Target]]:empty }.
- Return
- Let obj be !
ToObject (exprValue). - Let iterator be
EnumerateObjectProperties (obj). - Let nextMethod be !
GetV (iterator,"next" ). - Return the
Iterator Record { [[Iterator]]: iterator, [[NextMethod]]: nextMethod, [[Done]]:false }.
- If exprValue is either
- Else,
Assert : iterationKind is eitheriterate orasync-iterate .- If iterationKind is
async-iterate , let iteratorKind beasync . - Else, let iteratorKind be
sync . - Return ?
GetIterator (exprValue, iteratorKind).
14.7.5.7 ForIn/OfBodyEvaluation ( lhs, stmt, iteratorRecord, iterationKind, lhsKind, labelSet [ , iteratorKind ] )
The abstract operation ForIn/OfBodyEvaluation takes arguments lhs (a
- If iteratorKind is not present, set iteratorKind to
sync . - Let oldEnv be the
running execution context 's LexicalEnvironment. - Let V be
undefined . - Let destructuring be
IsDestructuring of lhs. - If destructuring is
true and lhsKind isassignment , thenAssert : lhs is aLeftHandSideExpression .- Let assignmentPattern be the
AssignmentPattern that iscovered by lhs.
- Repeat,
- Let nextResult be ?
Call (iteratorRecord.[[NextMethod]], iteratorRecord.[[Iterator]]). - If iteratorKind is
async , set nextResult to ?Await (nextResult). - If nextResult
is not an Object , throw aTypeError exception. - Let done be ?
IteratorComplete (nextResult). - If done is
true , return V. - Let nextValue be ?
IteratorValue (nextResult). - If lhsKind is either
assignment orvar-binding , then- If destructuring is
true , then- If lhsKind is
assignment , then- Let status be
Completion (DestructuringAssignmentEvaluation of assignmentPattern with argument nextValue).
- Let status be
- Else,
Assert : lhsKind isvar-binding .Assert : lhs is aForBinding .- Let status be
Completion (BindingInitialization of lhs with arguments nextValue andundefined ).
- If lhsKind is
- Else,
- Let lhsRef be
Completion (Evaluation of lhs). (It may be evaluated repeatedly.) - If lhsRef is an
abrupt completion , then- Let status be lhsRef.
- Else,
- Let status be
Completion (PutValue (lhsRef.[[Value]], nextValue)).
- Let status be
- Let lhsRef be
- If destructuring is
- Else,
Assert : lhsKind islexical-binding .Assert : lhs is aForDeclaration .- Let iterationEnv be
NewDeclarativeEnvironment (oldEnv). - Perform
ForDeclarationBindingInstantiation of lhs with argument iterationEnv. - Set the
running execution context 's LexicalEnvironment to iterationEnv. - If destructuring is
true , then- Let status be
Completion (ForDeclarationBindingInitialization of lhs with arguments nextValue and iterationEnv).
- Let status be
- Else,
Assert : lhs binds a single name.- Let lhsName be the sole element of the
BoundNames of lhs. - Let lhsRef be !
ResolveBinding (lhsName). - Let status be
Completion (InitializeReferencedBinding (lhsRef, nextValue)).
- If status is an
abrupt completion , then- Set the
running execution context 's LexicalEnvironment to oldEnv. - If iteratorKind is
async , return ?AsyncIteratorClose (iteratorRecord, status). - If iterationKind is
enumerate , then- Return ? status.
- Else,
Assert : iterationKind isiterate .- Return ?
IteratorClose (iteratorRecord, status).
- Set the
- Let result be
Completion (Evaluation of stmt). - Set the
running execution context 's LexicalEnvironment to oldEnv. - If
LoopContinues (result, labelSet) isfalse , then- If iterationKind is
enumerate , then- Return ?
UpdateEmpty (result, V).
- Return ?
- Else,
Assert : iterationKind isiterate .- Set status to
Completion (UpdateEmpty (result, V)). - If iteratorKind is
async , return ?AsyncIteratorClose (iteratorRecord, status). - Return ?
IteratorClose (iteratorRecord, status).
- If iterationKind is
- If result.[[Value]] is not
empty , set V to result.[[Value]].
- Let nextResult be ?
14.7.5.8 Runtime Semantics: Evaluation
- Let bindingId be the
StringValue ofBindingIdentifier . - Return ?
ResolveBinding (bindingId).
14.7.5.9 EnumerateObjectProperties ( O )
The abstract operation EnumerateObjectProperties takes argument O (an Object) and returns an
- Return an
iterator object whosenextmethod iterates over all the String-valued keys of enumerable properties of O. Theiterator object is never directly accessible to ECMAScript code. The mechanics and order of enumerating the properties is not specified but must conform to the rules specified below.
The throw and return methods are next method processes object properties to determine whether the next method is ignored. If new properties are added to the target object during enumeration, the newly added properties are not guaranteed to be processed in the active enumeration. A next method at most once in any enumeration.
Enumerating the properties of the target object includes enumerating properties of its prototype, and the prototype of the prototype, and so on, recursively; but a property of a prototype is not processed if it has the same name as a property that has already been processed by the next method. The values of [[Enumerable]] attributes are not considered when determining if a property of a prototype object has already been processed. The enumerable property names of prototype objects must be obtained by invoking EnumerateObjectProperties passing the prototype object as the argument. EnumerateObjectProperties must obtain the own
In addition, if neither O nor any object in its prototype chain is a
- the value of the [[Prototype]] internal slot of O or an object in its prototype chain changes,
- a property is removed from O or an object in its prototype chain,
- a property is added to an object in O's prototype chain, or
- the value of the [[Enumerable]] attribute of a property of O or an object in its prototype chain changes.
ECMAScript implementations are not required to implement the algorithm in
The following is an informative definition of an ECMAScript generator function that conforms to these rules:
function* EnumerateObjectProperties(obj) {
const visited = new Set();
for (const key of Reflect.ownKeys(obj)) {
if (typeof key === "symbol") continue;
const desc = Reflect.getOwnPropertyDescriptor(obj, key);
if (desc) {
visited.add(key);
if (desc.enumerable) yield key;
}
}
const proto = Reflect.getPrototypeOf(obj);
if (proto === null) return;
for (const protoKey of EnumerateObjectProperties(proto)) {
if (!visited.has(protoKey)) yield protoKey;
}
}
14.7.5.10 For-In Iterator Objects
A For-In Iterator is an object that represents a specific iteration over some specific object. For-In Iterator objects are never directly accessible to ECMAScript code; they exist solely to illustrate the behaviour of
14.7.5.10.1 CreateForInIterator ( object )
The abstract operation CreateForInIterator takes argument object (an Object) and returns a
- Let iterator be
OrdinaryObjectCreate (%ForInIteratorPrototype% , « [[Object]], [[ObjectWasVisited]], [[VisitedKeys]], [[RemainingKeys]] »). - Set iterator.[[Object]] to object.
- Set iterator.[[ObjectWasVisited]] to
false . - Set iterator.[[VisitedKeys]] to a new empty
List . - Set iterator.[[RemainingKeys]] to a new empty
List . - Return iterator.
14.7.5.10.2 The %ForInIteratorPrototype% Object
The
- has properties that are inherited by all
For-In Iterator objects . - is an
ordinary object . - has a [[Prototype]] internal slot whose value is
%Iterator.prototype% . - is never directly accessible to ECMAScript code.
- has the following properties:
14.7.5.10.2.1 %ForInIteratorPrototype% .next ( )
- Let O be the
this value. Assert : Ois an Object .Assert : O has all of the internal slots of aFor-In Iterator instance (14.7.5.10.3 ).- Let object be O.[[Object]].
- Repeat,
- If O.[[ObjectWasVisited]] is
false , then- Let keys be ?
object.[[OwnPropertyKeys]]() . - For each element key of keys, do
- If key
is a String , then- Append key to O.[[RemainingKeys]].
- If key
- Set O.[[ObjectWasVisited]] to
true .
- Let keys be ?
- Repeat, while O.[[RemainingKeys]] is not empty,
- Let r be the first element of O.[[RemainingKeys]].
- Remove the first element from O.[[RemainingKeys]].
- If O.[[VisitedKeys]] does not contain r, then
- Let desc be ?
object.[[GetOwnProperty]] (r). - If desc is not
undefined , then- Append r to O.[[VisitedKeys]].
- If desc.[[Enumerable]] is
true , returnCreateIteratorResultObject (r,false ).
- Let desc be ?
- Set object to ?
object.[[GetPrototypeOf]]() . - Set O.[[Object]] to object.
- Set O.[[ObjectWasVisited]] to
false . - If object is
null , returnCreateIteratorResultObject (undefined ,true ).
- If O.[[ObjectWasVisited]] is
14.7.5.10.3 Properties of For-In Iterator Instances
| Internal Slot | Type | Description |
|---|---|---|
| [[Object]] | an Object | The Object value whose properties are being iterated. |
| [[ObjectWasVisited]] | a Boolean |
|
| [[VisitedKeys]] |
a |
The values that have been emitted by this |
| [[RemainingKeys]] |
a |
The values remaining to be emitted for the current object, before iterating the properties of its prototype (if its prototype is not |
14.8 The continue Statement
Syntax
14.8.1 Static Semantics: Early Errors
-
It is a Syntax Error if this
ContinueStatement is not nested, directly or indirectly (but not crossing function orstaticinitialization block boundaries), within anIterationStatement .
14.8.2 Runtime Semantics: Evaluation
- Return
Completion Record { [[Type]]:continue , [[Value]]:empty , [[Target]]:empty }.
- Let label be the
StringValue ofLabelIdentifier . - Return
Completion Record { [[Type]]:continue , [[Value]]:empty , [[Target]]: label }.
14.9 The break Statement
Syntax
14.9.1 Static Semantics: Early Errors
-
It is a Syntax Error if this
BreakStatement is not nested, directly or indirectly (but not crossing function orstaticinitialization block boundaries), within anIterationStatement or aSwitchStatement .
14.9.2 Runtime Semantics: Evaluation
- Return
Completion Record { [[Type]]:break , [[Value]]:empty , [[Target]]:empty }.
- Let label be the
StringValue ofLabelIdentifier . - Return
Completion Record { [[Type]]:break , [[Value]]:empty , [[Target]]: label }.
14.10 The return Statement
Syntax
A return statement causes a function to cease execution and, in most cases, returns a value to the caller. If return statement may not actually return a value to the caller depending on surrounding context. For example, in a try block, a return statement's finally block.
14.10.1 Runtime Semantics: Evaluation
- Return
ReturnCompletion (undefined ).
- Let exprRef be ?
Evaluation ofExpression . - Let exprValue be ?
GetValue (exprRef). - If
GetGeneratorKind () isasync , set exprValue to ?Await (exprValue). - Return
ReturnCompletion (exprValue).
14.11 The with Statement
Use of the with statement is discouraged in new ECMAScript code. Consider alternatives that are permitted in both
Syntax
The with statement adds an
14.11.1 Static Semantics: Early Errors
-
It is a Syntax Error if
IsStrict (this production) istrue . -
It is a Syntax Error if
IsLabelledFunction (Statement ) istrue .
It is only necessary to apply the second rule if the extension specified in
14.11.2 Runtime Semantics: Evaluation
- Let val be ?
Evaluation ofExpression . - Let obj be ?
ToObject (?GetValue (val)). - Let oldEnv be the
running execution context 's LexicalEnvironment. - Let newEnv be
NewObjectEnvironment (obj,true , oldEnv). - Set the
running execution context 's LexicalEnvironment to newEnv. - Let C be
Completion (Evaluation ofStatement ). - Set the
running execution context 's LexicalEnvironment to oldEnv. - Return ?
UpdateEmpty (C,undefined ).
No matter how control leaves the embedded
14.12 The switch Statement
Syntax
14.12.1 Static Semantics: Early Errors
-
It is a Syntax Error if the
LexicallyDeclaredNames ofCaseBlock contains any duplicate entries. -
It is a Syntax Error if any element of the
LexicallyDeclaredNames ofCaseBlock also occurs in theVarDeclaredNames ofCaseBlock .
14.12.2 Runtime Semantics: CaseBlockEvaluation
The
- Return
undefined .
- Let V be
undefined . - Let A be the
List ofCaseClause items inCaseClauses , in source text order. - Let found be
false . - For each
CaseClause C of A, do- If found is
false , then- Set found to ?
CaseClauseIsSelected (C, input).
- Set found to ?
- If found is
true , then- Let R be
Completion (Evaluation of C). - If R.[[Value]] is not
empty , set V to R.[[Value]]. - If R is an
abrupt completion , return ?UpdateEmpty (R, V).
- Let R be
- If found is
- Return V.
- Let V be
undefined . - If the first
CaseClauses is present, then- Let A be the
List ofCaseClause items in the firstCaseClauses , in source text order.
- Let A be the
- Else,
- Let A be a new empty
List .
- Let A be a new empty
- Let found be
false . - For each
CaseClause C of A, do- If found is
false , then- Set found to ?
CaseClauseIsSelected (C, input).
- Set found to ?
- If found is
true , then- Let R be
Completion (Evaluation of C). - If R.[[Value]] is not
empty , set V to R.[[Value]]. - If R is an
abrupt completion , return ?UpdateEmpty (R, V).
- Let R be
- If found is
- Let foundInB be
false . - If the second
CaseClauses is present, then- Let B be the
List ofCaseClause items in the secondCaseClauses , in source text order.
- Let B be the
- Else,
- Let B be a new empty
List .
- Let B be a new empty
- If found is
false , then- For each
CaseClause C of B, do- If foundInB is
false , then- Set foundInB to ?
CaseClauseIsSelected (C, input).
- Set foundInB to ?
- If foundInB is
true , then- Let R be
Completion (Evaluation ofCaseClause C). - If R.[[Value]] is not
empty , set V to R.[[Value]]. - If R is an
abrupt completion , return ?UpdateEmpty (R, V).
- Let R be
- If foundInB is
- For each
- If foundInB is
true , return V. - Let defaultR be
Completion (Evaluation ofDefaultClause ). - If defaultR.[[Value]] is not
empty , set V to defaultR.[[Value]]. - If defaultR is an
abrupt completion , return ?UpdateEmpty (defaultR, V). - NOTE: The following is another complete iteration of the second
CaseClauses . - For each
CaseClause C of B, do- Let R be
Completion (Evaluation ofCaseClause C). - If R.[[Value]] is not
empty , set V to R.[[Value]]. - If R is an
abrupt completion , return ?UpdateEmpty (R, V).
- Let R be
- Return V.
14.12.3 CaseClauseIsSelected ( C, input )
The abstract operation CaseClauseIsSelected takes arguments C (a
Assert : C is an instance of the production .CaseClause : case Expression : StatementList opt - Let exprRef be ?
Evaluation of theExpression of C. - Let clauseSelector be ?
GetValue (exprRef). - Return
IsStrictlyEqual (input, clauseSelector).
This operation does not execute C's
14.12.4 Runtime Semantics: Evaluation
- Let exprRef be ?
Evaluation ofExpression . - Let switchValue be ?
GetValue (exprRef). - Let oldEnv be the
running execution context 's LexicalEnvironment. - Let blockEnv be
NewDeclarativeEnvironment (oldEnv). - Perform
BlockDeclarationInstantiation (CaseBlock , blockEnv). - Set the
running execution context 's LexicalEnvironment to blockEnv. - Let R be
Completion (CaseBlockEvaluation ofCaseBlock with argument switchValue). - Set the
running execution context 's LexicalEnvironment to oldEnv. - Return R.
No matter how control leaves the
- Return
empty .
- Return ?
Evaluation ofStatementList .
- Return
empty .
- Return ?
Evaluation ofStatementList .
14.13 Labelled Statements
Syntax
A break and continue statements. ECMAScript has no goto statement. A
14.13.1 Static Semantics: Early Errors
- It is a Syntax Error if any source text is matched by this production.
An alternative definition for this rule is provided in
14.13.2 Static Semantics: IsLabelledFunction ( stmt )
The abstract operation IsLabelledFunction takes argument stmt (a
- If stmt is not a
LabelledStatement , returnfalse . - Let item be the
LabelledItem of stmt. - If item is
, returnLabelledItem : FunctionDeclaration true . - Let subStmt be the
Statement of item. - Return IsLabelledFunction(subStmt).
14.13.3 Runtime Semantics: Evaluation
- Return ?
LabelledEvaluation of thisLabelledStatement with argument « ».
14.13.4 Runtime Semantics: LabelledEvaluation
The
- Let stmtResult be
Completion (LoopEvaluation ofIterationStatement with argument labelSet). - If stmtResult is a
break completion , then- If stmtResult.[[Target]] is
empty , then- If stmtResult.[[Value]] is
empty , set stmtResult toNormalCompletion (undefined ). - Else, set stmtResult to
NormalCompletion (stmtResult.[[Value]]).
- If stmtResult.[[Value]] is
- If stmtResult.[[Target]] is
- Return ? stmtResult.
- Let stmtResult be
Completion (Evaluation ofSwitchStatement ). - If stmtResult is a
break completion , then- If stmtResult.[[Target]] is
empty , then- If stmtResult.[[Value]] is
empty , set stmtResult toNormalCompletion (undefined ). - Else, set stmtResult to
NormalCompletion (stmtResult.[[Value]]).
- If stmtResult.[[Value]] is
- If stmtResult.[[Target]] is
- Return ? stmtResult.
A
- Let label be the
StringValue ofLabelIdentifier . - Let newLabelSet be the
list-concatenation of labelSet and « label ». - Let stmtResult be
Completion (LabelledEvaluation ofLabelledItem with argument newLabelSet). - If stmtResult is a
break completion and stmtResult.[[Target]] is label, then- Set stmtResult to
NormalCompletion (stmtResult.[[Value]]).
- Set stmtResult to
- Return ? stmtResult.
- Return ?
Evaluation ofFunctionDeclaration .
- Return ?
Evaluation ofStatement .
The only two productions of
14.14 The throw Statement
Syntax
14.14.1 Runtime Semantics: Evaluation
- Let exprRef be ?
Evaluation ofExpression . - Let exprValue be ?
GetValue (exprRef). - Return
ThrowCompletion (exprValue).
14.15 The try Statement
Syntax
The try statement encloses a block of code in which an exceptional condition can occur, such as a runtime error or a throw statement. The catch clause provides the exception-handling code. When a catch clause catches an exception, its
14.15.1 Static Semantics: Early Errors
-
It is a Syntax Error if the
BoundNames ofCatchParameter contains any duplicate elements. -
It is a Syntax Error if any element of the
BoundNames ofCatchParameter also occurs in theLexicallyDeclaredNames ofBlock . -
It is a Syntax Error if any element of the
BoundNames ofCatchParameter also occurs in theVarDeclaredNames ofBlock .
An alternative
14.15.2 Runtime Semantics: CatchClauseEvaluation
The
- Let oldEnv be the
running execution context 's LexicalEnvironment. - Let catchEnv be
NewDeclarativeEnvironment (oldEnv). - For each element argName of the
BoundNames ofCatchParameter , do- Perform ! catchEnv.CreateMutableBinding(argName,
false ).
- Perform ! catchEnv.CreateMutableBinding(argName,
- Set the
running execution context 's LexicalEnvironment to catchEnv. - Let status be
Completion (BindingInitialization ofCatchParameter with arguments thrownValue and catchEnv). - If status is an
abrupt completion , then- Set the
running execution context 's LexicalEnvironment to oldEnv. - Return ? status.
- Set the
- Let B be
Completion (Evaluation ofBlock ). - Set the
running execution context 's LexicalEnvironment to oldEnv. - Return ? B.
- Return ?
Evaluation ofBlock .
No matter how control leaves the
14.15.3 Runtime Semantics: Evaluation
- Let B be
Completion (Evaluation ofBlock ). - If B is a
throw completion , let C beCompletion (CatchClauseEvaluation ofCatch with argument B.[[Value]]). - Else, let C be B.
- Return ?
UpdateEmpty (C,undefined ).
- Let B be
Completion (Evaluation ofBlock ). - Let F be
Completion (Evaluation ofFinally ). - If F is a
normal completion , set F to B. - Return ?
UpdateEmpty (F,undefined ).
- Let B be
Completion (Evaluation ofBlock ). - If B is a
throw completion , let C beCompletion (CatchClauseEvaluation ofCatch with argument B.[[Value]]). - Else, let C be B.
- Let F be
Completion (Evaluation ofFinally ). - If F is a
normal completion , set F to C. - Return ?
UpdateEmpty (F,undefined ).
14.16 The debugger Statement
Syntax
14.16.1 Runtime Semantics: Evaluation
Evaluating a
- If an
implementation-defined debugging facility is available and enabled, then- Perform an
implementation-defined debugging action. - Return a new
implementation-defined Completion Record .
- Perform an
- Else,
- Return
empty .
- Return