12.2 主值表达式

语法

  1. PrimaryExpression[Yield]:
  2. this
  3. IdentifierReference[?Yield]
  4. Literal
  5. ArrayLiteral[?Yield]
  6. ObjectLiteral[?Yield]
  7. FunctionExpression
  8. ClassExpression[?Yield]
  9. GeneratorExpression
  10. RegularExpressionLiteral
  11. TemplateLiteral[?Yield]
  12. CoverParenthesizedExpressionAndArrowParameterList[?Yield]
  13. CoverParenthesizedExpressionAndArrowParameterList[Yield]:
  14. (Expression[In, ?Yield])
  15. ()
  16. (...BindingIdentifier[?Yield])
  17. (...BindingPattern[?Yield])
  18. (Expression[In, ?Yield],...BindingIdentifier[?Yield])
  19. (Expression[In, ?Yield],...BindingPattern[?Yield])

补充语法:

当执行如下产生式:

PrimaryExpression : CoverParenthesizedExpressionAndArrowParameterList

解释这句可以优雅的用如下语法来解释:

  1. ParenthesizedExpression[Yield]:
  2. (Expression[In, ?Yield])

12.2.1 语法

12.2.1.1 静态语法: CoveredParenthesizedExpression

CoverParenthesizedExpressionAndArrowParameterList:(Expression)

  1. Return the result of parsing the lexical token stream matched by CoverParenthesizedExpressionAndArrowParameterList [Yield] using either ParenthesizedExpression or ParenthesizedExpression[Yield] as the goal symbol depending upon whether the [Yield] grammar parameter was present when CoverParenthesizedExpressionAndArrowParameterList was matched.

12.2.1.2 静态语法:HasName

  1. PrimaryExpression:CoverParenthesizedExpressionAndArrowParameterList
  2. Let expr be CoveredParenthesizedExpression of CoverParenthesizedExpressionAndArrowParameterList.
  3. If IsFunctionDefinition of expr is false, return false.
  4. Return HasName of expr.

12.2.1.3 静态语法:IsFunctionDefinition

  1. PrimaryExpression:
  2. this
  3. IdentifierReference
  4. Literal
  5. ArrayLiteral
  6. ObjectLiteral
  7. RegularExpressionLiteral
  8. TemplateLiteral
  9. Return false.
  10. PrimaryExpression:CoverParenthesizedExpressionAndArrowParameterList
  11. Let expr be CoveredParenthesizedExpression of CoverParenthesizedExpressionAndArrowParameterList.
  12. Return IsFunctionDefinition of expr.

12.2.1.4 静态语法:IsIdentifierRef

  1. PrimaryExpression:IdentifierReference
  2. Return true.
  3. PrimaryExpression:
  4. this
  5. Literal
  6. ArrayLiteral
  7. ObjectLiteral
  8. FunctionExpression
  9. ClassExpression
  10. GeneratorExpression
  11. RegularExpressionLiteral
  12. TemplateLiteral
  13. CoverParenthesizedExpressionAndArrowParameterList
  14. Return false.

12.2.1.5 静态语法:IsValidSimpleAssignmentTarget

  1. PrimaryExpression:
  2. this
  3. Literal
  4. ArrayLiteral
  5. ObjectLiteral
  6. FunctionExpression
  7. ClassExpression
  8. GeneratorExpression
  9. RegularExpressionLiteral
  10. TemplateLiteral
  11. Return false.
  12. PrimaryExpression:CoverParenthesizedExpressionAndArrowParameterList
  13. Let expr be CoveredParenthesizedExpression of CoverParenthesizedExpressionAndArrowParameterList.
  14. Return IsValidSimpleAssignmentTarget of expr.

12.2.2 this关键字

12.2.2.1 Runtime Semantics: Evaluation

  1. PrimaryExpression:this
  2. Return ? ResolveThisBinding( ).

12.2.3 标识符引用

查看12.1中的IdentifierReference

12.2.4 字面量

  1. Syntax
  2. Literal:
  3. NullLiteral
  4. BooleanLiteral
  5. NumericLiteral
  6. StringLiteral

12.2.4.1 Runtime Semantics: Evaluation

  1. Literal:NullLiteral
  2. 返回 null.
  3. Literal:BooleanLiteral
  4. 返回 false 如果 BooleanLiteral false token.
  5. 返回 true 如果 BooleanLiteral true token.
  6. Literal:NumericLiteral
  7. 返回 11.8.3中所定义的那样NumericLiteralMV值.
  8. Literal:StringLiteral
  9. 返回 11.8.4.2中所定义的那样StringLiteral的字符串值.

12.2.5 数组初始化

注意:一个数组字面量是一个表达式,用来描述Array对象的初始化。用一个列表,0个或者多个由表达式表示的元素,用方括号包裹着。列表中的元素不一定要是字面量,他们会在数组初始化执行的时候被执行。

数组元素可以在开始、中间、结尾任何地方省略。无论何时在元素列表中出现逗号,只要不是在AssignmentExpression之后的逗号(比如在一个逗号后的逗号),会有一个省略元素,这个省略的元素也会贡献长度,并且增长下一个元素的索引。省略的元素不会被定义。如果省略的元素是在数组的最后,那么这个元素不会贡献长度。

语法

  1. ArrayLiteral[Yield]:
  2. [Elisionopt]
  3. [ElementList[?Yield]]
  4. [ElementList[?Yield],Elisionopt]
  5. ElementList[Yield]:
  6. ElisionoptAssignmentExpression[In, ?Yield]
  7. ElisionoptSpreadElement[?Yield]
  8. ElementList[?Yield],ElisionoptAssignmentExpression[In, ?Yield]
  9. ElementList[?Yield],ElisionoptSpreadElement[?Yield]
  10. Elision:
  11. ,
  12. Elision,
  13. SpreadElement[Yield]:
  14. ...AssignmentExpression[In, ?Yield]

12.2.5.1 静态语法:ElisionWidth

  1. Elision:,
  2. Return the numeric value 1.
  3. Elision:Elision,
  4. Let preceding be the ElisionWidth of Elision.
  5. Return preceding+1.

12.2.5.2 运行时语法:ArrayAccumulation

  1. With parameters array and nextIndex.
  2. ElementList:ElisionAssignmentExpression
  3. Let padding be the ElisionWidth of Elision; if Elision is not present, use the numeric value zero.
  4. Let initResult be the result of evaluating AssignmentExpression.
  5. Let initValue be ? GetValue(initResult).
  6. Let created be CreateDataProperty(array, ToString(ToUint32(nextIndex+padding)), initValue).
  7. Assert: created is true.
  8. Return nextIndex+padding+1.
  9. ElementList:ElisionSpreadElement
  10. Let padding be the ElisionWidth of Elision; if Elision is not present, use the numeric value zero.
  11. Return the result of performing ArrayAccumulation for SpreadElement with arguments array and nextIndex+padding.
  12. ElementList:ElementList,ElisionAssignmentExpression
  13. Let postIndex be the result of performing ArrayAccumulation for ElementList with arguments array and nextIndex.
  14. ReturnIfAbrupt(postIndex).
  15. Let padding be the ElisionWidth of Elision; if Elision is not present, use the numeric value zero.
  16. Let initResult be the result of evaluating AssignmentExpression.
  17. Let initValue be ? GetValue(initResult).
  18. Let created be CreateDataProperty(array, ToString(ToUint32(postIndex+padding)), initValue).
  19. Assert: created is true.
  20. Return postIndex+padding+1.
  21. ElementList:ElementList,ElisionSpreadElement
  22. Let postIndex be the result of performing ArrayAccumulation for ElementList with arguments array and nextIndex.
  23. ReturnIfAbrupt(postIndex).
  24. Let padding be the ElisionWidth of Elision; if Elision is not present, use the numeric value zero.
  25. Return the result of performing ArrayAccumulation for SpreadElement with arguments array and postIndex+padding.
  26. SpreadElement:...AssignmentExpression
  27. Let spreadRef be the result of evaluating AssignmentExpression.
  28. Let spreadObj be ? GetValue(spreadRef).
  29. Let iterator be ? GetIterator(spreadObj).
  30. Repeat
  31. Let next be ? IteratorStep(iterator).
  32. If next is false, return nextIndex.
  33. Let nextValue be ? IteratorValue(next).
  34. Let status be CreateDataProperty(array, ToString(ToUint32(nextIndex)), nextValue).
  35. Assert: status is true.
  36. Let nextIndex be nextIndex + 1.

注意:CreateDataProperty是用来确保array能定义自身的属性,就算内置的Array prototype通过[[Set]]修改,阻止创建自身属性。

12.2.5.3 运行时语法:Evaluation

  1. ArrayLiteral:[Elision]
  2. Let array be ArrayCreate(0).
  3. Let pad be the ElisionWidth of Elision; if Elision is not present, use the numeric value zero.
  4. Perform Set(array, "length", ToUint32(pad), false).
  5. NOTE: The above Set cannot fail because of the nature of the object returned by ArrayCreate.
  6. Return array.
  7. ArrayLiteral:[ElementList]
  8. Let array be ArrayCreate(0).
  9. Let len be the result of performing ArrayAccumulation for ElementList with arguments array and 0.
  10. ReturnIfAbrupt(len).
  11. Perform Set(array, "length", ToUint32(len), false).
  12. NOTE: The above Set cannot fail because of the nature of the object returned by ArrayCreate.
  13. Return array.
  14. ArrayLiteral:[ElementList,Elision]
  15. Let array be ArrayCreate(0).
  16. Let len be the result of performing ArrayAccumulation for ElementList with arguments array and 0.
  17. ReturnIfAbrupt(len).
  18. Let padding be the ElisionWidth of Elision; if Elision is not present, use the numeric value zero.
  19. Perform Set(array, "length", ToUint32(padding+len), false).
  20. NOTE: The above Set cannot fail because of the nature of the object returned by ArrayCreate.
  21. Return array.

12.2.6 对象初始化

注意1:对象初始化是一个以直接量的方式描述对象的初始化过程的表达式。它是用花括号括起来的由零或者多对属性名 / 关联值组成的列表,值不需要是直接量,每次对象初始化被执行到时他们会执行一次。

语法

  1. ObjectLiteral[Yield]:
  2. {}
  3. {PropertyDefinitionList[?Yield]}
  4. {PropertyDefinitionList[?Yield],}
  5. PropertyDefinitionList[Yield]:
  6. PropertyDefinition[?Yield]
  7. PropertyDefinitionList[?Yield],PropertyDefinition[?Yield]
  8. PropertyDefinition[Yield]:
  9. IdentifierReference[?Yield]
  10. CoverInitializedName[?Yield]
  11. PropertyName[?Yield]:AssignmentExpression[In, ?Yield]
  12. MethodDefinition[?Yield]
  13. PropertyName[Yield]:
  14. LiteralPropertyName
  15. ComputedPropertyName[?Yield]
  16. LiteralPropertyName:
  17. IdentifierName
  18. StringLiteral
  19. NumericLiteral
  20. ComputedPropertyName[Yield]:
  21. [AssignmentExpression[In, ?Yield]]
  22. CoverInitializedName[Yield]:
  23. IdentifierReference[?Yield]Initializer[In, ?Yield]
  24. Initializer[In, Yield]:
  25. =AssignmentExpression[?In, ?Yield]

注意2:MethodDefinition定义在14.3

注意3:在平常的上下文中,ObjectLiteral是用来当做包裹语法的,作为一种更严格的二级语法。CoverInitializedName产生式是用来确保覆盖这种二级语法的。然而当时期望一个真实的对象字面量时,这个产生式的会产生早期错误。

12.2.6.1 静态语法:早期错误

  1. PropertyDefinition:MethodDefinition
  • 如果MethodDefinition执行HasDirectSuper的结果为true,这将是一个语法错误

ObjectLiteral产生式除了可以表达对象初始化,也能被用作一种给ObjectAssignmentPattern的包裹语法。并且也可能被当作CoverParenthesizedExpressionAndArrowParameterList的一部分。当ObjectLiteral是在一个需要ObjectAssignmentPattern的上下文中的时候,将不会应用接下来的早期错误规则,当然它们也不会应用在CoverParenthesizedExpressionAndArrowParameterList的初始化解析中。

  1. PropertyDefinition:CoverInitializedName
  • 如果代码匹配了这个产生式,那么将总会抛出语法错误。

注意: 由于存在这个产生式,ObjectLiteral将只能作为ObjectAssignmentPattern的包裹语法。不能出现在真实的对象初始化中。

12.2.6.2 静态语法:ComputedPropertyContains

  1. With parameter symbol.
  2. PropertyName:LiteralPropertyName
  3. Return false.
  4. PropertyName:ComputedPropertyName
  5. Return the result of ComputedPropertyName Contains symbol.

12.2.6.3 静态语法:Contains

  1. With parameter symbol.
  2. PropertyDefinition:MethodDefinition
  3. If symbol is MethodDefinition, return true.
  4. Return the result of ComputedPropertyContains for MethodDefinition with argument symbol.
  5. NOTE Static semantic rules that depend upon substructure generally do not look into function definitions.
  6. LiteralPropertyName:IdentifierName
  7. If symbol is a ReservedWord, return false.
  8. If symbol is an Identifier and StringValue of symbol is the same value as the StringValue of IdentifierName, return true.
  9. Return false.

12.2.6.4 静态语法:HasComputedPropertyKey

  1. PropertyDefinitionList:PropertyDefinitionList,PropertyDefinition
  2. If HasComputedPropertyKey of PropertyDefinitionList is true, return true.
  3. Return HasComputedPropertyKey of PropertyDefinition.
  4. PropertyDefinition:IdentifierReference
  5. Return false.
  6. PropertyDefinition:PropertyName:AssignmentExpression
  7. Return IsComputedPropertyKey of PropertyName.

12.2.6.5 静态语法:IsComputedPropertyKey

  1. PropertyName:LiteralPropertyName
  2. Return false.
  3. PropertyName:ComputedPropertyName
  4. Return true.

12.2.6.6 静态语法:PropName

  1. PropertyDefinition:IdentifierReference
  2. Return StringValue of IdentifierReference.
  3. PropertyDefinition:PropertyName:AssignmentExpression
  4. Return PropName of PropertyName.
  5. LiteralPropertyName:IdentifierName
  6. Return StringValue of IdentifierName.
  7. LiteralPropertyName:StringLiteral
  8. Return a String value whose code units are the SV of the StringLiteral.
  9. LiteralPropertyName:NumericLiteral
  10. Let nbr be the result of forming the value of the NumericLiteral.
  11. Return ! ToString(nbr).
  12. ComputedPropertyName:[AssignmentExpression]
  13. Return empty.

12.2.6.7 静态语法:PropertyNameList

  1. PropertyDefinitionList:PropertyDefinition
  2. If PropName of PropertyDefinition is empty, return a new empty List.
  3. Return a new List containing PropName of PropertyDefinition.
  4. PropertyDefinitionList:PropertyDefinitionList,PropertyDefinition
  5. Let list be PropertyNameList of PropertyDefinitionList.
  6. If PropName of PropertyDefinition is empty, return list.
  7. Append PropName of PropertyDefinition to the end of list.
  8. Return list.

12.2.6.8 运行时语法:Evaluation

  1. ObjectLiteral:{}
  2. Return ObjectCreate(%ObjectPrototype%).
  3. ObjectLiteral:
  4. {PropertyDefinitionList}
  5. {PropertyDefinitionList,}
  6. Let obj be ObjectCreate(%ObjectPrototype%).
  7. Let status be the result of performing PropertyDefinitionEvaluation of PropertyDefinitionList with arguments obj and true.
  8. ReturnIfAbrupt(status).
  9. Return obj.
  10. LiteralPropertyName:IdentifierName
  11. Return StringValue of IdentifierName.
  12. LiteralPropertyName:StringLiteral
  13. Return a String value whose code units are the SV of the StringLiteral.
  14. LiteralPropertyName:NumericLiteral
  15. Let nbr be the result of forming the value of the NumericLiteral.
  16. Return ! ToString(nbr).
  17. ComputedPropertyName:[AssignmentExpression]
  18. Let exprValue be the result of evaluating AssignmentExpression.
  19. Let propName be ? GetValue(exprValue).
  20. Return ? ToPropertyKey(propName).

12.2.6.9 运行时语法:PropertyDefinitionEvaluation

  1. With parameters object and enumerable.
  2. PropertyDefinitionList:PropertyDefinitionList,PropertyDefinition
  3. Let status be the result of performing PropertyDefinitionEvaluation of PropertyDefinitionList with arguments object and enumerable.
  4. ReturnIfAbrupt(status).
  5. Return the result of performing PropertyDefinitionEvaluation of PropertyDefinition with arguments object and enumerable.
  6. PropertyDefinition:IdentifierReference
  7. Let propName be StringValue of IdentifierReference.
  8. Let exprValue be the result of evaluating IdentifierReference.
  9. Let propValue be ? GetValue(exprValue).
  10. Assert: enumerable is true.
  11. Return CreateDataPropertyOrThrow(object, propName, propValue).
  12. PropertyDefinition:PropertyName:AssignmentExpression
  13. Let propKey be the result of evaluating PropertyName.
  14. ReturnIfAbrupt(propKey).
  15. Let exprValueRef be the result of evaluating AssignmentExpression.
  16. Let propValue be ? GetValue(exprValueRef).
  17. If IsAnonymousFunctionDefinition(AssignmentExpression) is true, then
  18. Let hasNameProperty be ? HasOwnProperty(propValue, "name").
  19. If hasNameProperty is false, perform SetFunctionName(propValue, propKey).
  20. Assert: enumerable is true.
  21. Return CreateDataPropertyOrThrow(object, propKey, propValue).
  22. NOTE An alternative semantics for this production is given in B.3.1.

12.2.7 Function Defining Expressions

See14.1forPrimaryExpression:FunctionExpression.

See14.4forPrimaryExpression:GeneratorExpression.

See14.5forPrimaryExpression:ClassExpression.

12.2.8 Regular Expression Literals

语法

See11.8.5.

12.2.8.1 静态语法:早期错误

  1. PrimaryExpression:RegularExpressionLiteral
  2. It is a Syntax Error if BodyText of RegularExpressionLiteral cannot be recognized using the goal symbol Pattern of the ECMAScript RegExp grammar specified in 21.2.1.
  3. It is a Syntax Error if FlagText of RegularExpressionLiteral contains any code points other than "g", "i", "m", "u", or "y", or if it contains the same code point more than once.

12.2.8.2 运行时语法:Evaluation

  1. PrimaryExpression:RegularExpressionLiteral
  2. Let pattern be the String value consisting of the UTF16Encoding of each code point of BodyText of RegularExpressionLiteral.
  3. Let flags be the String value consisting of the UTF16Encoding of each code point of FlagText of RegularExpressionLiteral.
  4. Return RegExpCreate(pattern, flags).