12.2 主值表达式

语法

PrimaryExpression[Yield]:
    this
    IdentifierReference[?Yield]
    Literal
    ArrayLiteral[?Yield]
    ObjectLiteral[?Yield]
    FunctionExpression
    ClassExpression[?Yield]
    GeneratorExpression
    RegularExpressionLiteral
    TemplateLiteral[?Yield]
CoverParenthesizedExpressionAndArrowParameterList[?Yield]
    CoverParenthesizedExpressionAndArrowParameterList[Yield]:
    (Expression[In, ?Yield])
    ()
    (...BindingIdentifier[?Yield])
    (...BindingPattern[?Yield])
    (Expression[In, ?Yield],...BindingIdentifier[?Yield])
    (Expression[In, ?Yield],...BindingPattern[?Yield])

补充语法:

当执行如下产生式:

PrimaryExpression : CoverParenthesizedExpressionAndArrowParameterList

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

ParenthesizedExpression[Yield]:
    (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

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

12.2.1.3 静态语法:IsFunctionDefinition

PrimaryExpression:
    this
    IdentifierReference
    Literal
    ArrayLiteral
    ObjectLiteral
    RegularExpressionLiteral
    TemplateLiteral
Return false.

PrimaryExpression:CoverParenthesizedExpressionAndArrowParameterList
    Let expr be CoveredParenthesizedExpression of CoverParenthesizedExpressionAndArrowParameterList.
    Return IsFunctionDefinition of expr.

12.2.1.4 静态语法:IsIdentifierRef

PrimaryExpression:IdentifierReference
    Return true.
        PrimaryExpression:
            this
            Literal
            ArrayLiteral
            ObjectLiteral
            FunctionExpression
            ClassExpression
            GeneratorExpression
            RegularExpressionLiteral
            TemplateLiteral
            CoverParenthesizedExpressionAndArrowParameterList
        Return false.

12.2.1.5 静态语法:IsValidSimpleAssignmentTarget

PrimaryExpression:
    this
    Literal
    ArrayLiteral
    ObjectLiteral
    FunctionExpression
    ClassExpression
    GeneratorExpression
    RegularExpressionLiteral
    TemplateLiteral
Return false.

PrimaryExpression:CoverParenthesizedExpressionAndArrowParameterList
    Let expr be CoveredParenthesizedExpression of CoverParenthesizedExpressionAndArrowParameterList.
    Return IsValidSimpleAssignmentTarget of expr.

12.2.2 this关键字

12.2.2.1 Runtime Semantics: Evaluation

PrimaryExpression:this
Return ? ResolveThisBinding( ).

12.2.3 标识符引用

查看12.1中的IdentifierReference

12.2.4 字面量

Syntax
    Literal:
    NullLiteral
    BooleanLiteral
    NumericLiteral
    StringLiteral

12.2.4.1 Runtime Semantics: Evaluation

Literal:NullLiteral
    返回 null.
Literal:BooleanLiteral
    返回 false 如果 BooleanLiteral 为 false token.
    返回 true 如果 BooleanLiteral 为 true token.
Literal:NumericLiteral
    返回 如 11.8.3中所定义的那样NumericLiteral的MV值.
Literal:StringLiteral
    返回 如 11.8.4.2中所定义的那样StringLiteral的字符串值.

12.2.5 数组初始化

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

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

语法

ArrayLiteral[Yield]:
    [Elisionopt]
    [ElementList[?Yield]]
    [ElementList[?Yield],Elisionopt]
ElementList[Yield]:
    ElisionoptAssignmentExpression[In, ?Yield]
    ElisionoptSpreadElement[?Yield]
    ElementList[?Yield],ElisionoptAssignmentExpression[In, ?Yield]
    ElementList[?Yield],ElisionoptSpreadElement[?Yield]
Elision:
    ,
    Elision,
SpreadElement[Yield]:
    ...AssignmentExpression[In, ?Yield]

12.2.5.1 静态语法:ElisionWidth

Elision:,
    Return the numeric value 1.
Elision:Elision,
    Let preceding be the ElisionWidth of Elision.
    Return preceding+1.

12.2.5.2 运行时语法:ArrayAccumulation

With parameters array and nextIndex.

ElementList:ElisionAssignmentExpression
    Let padding be the ElisionWidth of Elision; if Elision is not present, use the numeric value zero.
    Let initResult be the result of evaluating AssignmentExpression.
    Let initValue be ? GetValue(initResult).
    Let created be CreateDataProperty(array, ToString(ToUint32(nextIndex+padding)), initValue).
    Assert: created is true.
    Return nextIndex+padding+1.
ElementList:ElisionSpreadElement
    Let padding be the ElisionWidth of Elision; if Elision is not present, use the numeric value zero.
    Return the result of performing ArrayAccumulation for SpreadElement with arguments array and nextIndex+padding.
ElementList:ElementList,ElisionAssignmentExpression
    Let postIndex be the result of performing ArrayAccumulation for ElementList with arguments array and nextIndex.
    ReturnIfAbrupt(postIndex).
    Let padding be the ElisionWidth of Elision; if Elision is not present, use the numeric value zero.
    Let initResult be the result of evaluating AssignmentExpression.
    Let initValue be ? GetValue(initResult).
    Let created be CreateDataProperty(array, ToString(ToUint32(postIndex+padding)), initValue).
    Assert: created is true.
    Return postIndex+padding+1.
ElementList:ElementList,ElisionSpreadElement
    Let postIndex be the result of performing ArrayAccumulation for ElementList with arguments array and nextIndex.
    ReturnIfAbrupt(postIndex).
    Let padding be the ElisionWidth of Elision; if Elision is not present, use the numeric value zero.
    Return the result of performing ArrayAccumulation for SpreadElement with arguments array and postIndex+padding.
SpreadElement:...AssignmentExpression
    Let spreadRef be the result of evaluating AssignmentExpression.
    Let spreadObj be ? GetValue(spreadRef).
    Let iterator be ? GetIterator(spreadObj).
    Repeat
        Let next be ? IteratorStep(iterator).
        If next is false, return nextIndex.
        Let nextValue be ? IteratorValue(next).
        Let status be CreateDataProperty(array, ToString(ToUint32(nextIndex)), nextValue).
        Assert: status is true.
        Let nextIndex be nextIndex + 1.

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

12.2.5.3 运行时语法:Evaluation

ArrayLiteral:[Elision]
    Let array be ArrayCreate(0).
    Let pad be the ElisionWidth of Elision; if Elision is not present, use the numeric value zero.
    Perform Set(array, "length", ToUint32(pad), false).
    NOTE: The above Set cannot fail because of the nature of the object returned by ArrayCreate.
    Return array.
ArrayLiteral:[ElementList]
    Let array be ArrayCreate(0).
    Let len be the result of performing ArrayAccumulation for ElementList with arguments array and 0.
    ReturnIfAbrupt(len).
    Perform Set(array, "length", ToUint32(len), false).
    NOTE: The above Set cannot fail because of the nature of the object returned by ArrayCreate.
    Return array.
ArrayLiteral:[ElementList,Elision]
    Let array be ArrayCreate(0).
    Let len be the result of performing ArrayAccumulation for ElementList with arguments array and 0.
    ReturnIfAbrupt(len).
    Let padding be the ElisionWidth of Elision; if Elision is not present, use the numeric value zero.
    Perform Set(array, "length", ToUint32(padding+len), false).
    NOTE: The above Set cannot fail because of the nature of the object returned by ArrayCreate.
    Return array.

12.2.6 对象初始化

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

语法

ObjectLiteral[Yield]:
    {}
    {PropertyDefinitionList[?Yield]}
    {PropertyDefinitionList[?Yield],}
PropertyDefinitionList[Yield]:
    PropertyDefinition[?Yield]
    PropertyDefinitionList[?Yield],PropertyDefinition[?Yield]
PropertyDefinition[Yield]:
    IdentifierReference[?Yield]
    CoverInitializedName[?Yield]
PropertyName[?Yield]:AssignmentExpression[In, ?Yield]
    MethodDefinition[?Yield]
PropertyName[Yield]:
    LiteralPropertyName
    ComputedPropertyName[?Yield]
LiteralPropertyName:
    IdentifierName
    StringLiteral
    NumericLiteral
ComputedPropertyName[Yield]:
    [AssignmentExpression[In, ?Yield]]
CoverInitializedName[Yield]:
    IdentifierReference[?Yield]Initializer[In, ?Yield]
Initializer[In, Yield]:
    =AssignmentExpression[?In, ?Yield]

注意2:MethodDefinition定义在14.3

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

12.2.6.1 静态语法:早期错误

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

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

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

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

12.2.6.2 静态语法:ComputedPropertyContains

With parameter symbol.

PropertyName:LiteralPropertyName
    Return false.
PropertyName:ComputedPropertyName
    Return the result of ComputedPropertyName Contains symbol.

12.2.6.3 静态语法:Contains

With parameter symbol.

PropertyDefinition:MethodDefinition
    If symbol is MethodDefinition, return true.
    Return the result of ComputedPropertyContains for MethodDefinition with argument symbol.

NOTE Static semantic rules that depend upon substructure generally do not look into function definitions.

LiteralPropertyName:IdentifierName
    If symbol is a ReservedWord, return false.
    If symbol is an Identifier and StringValue of symbol is the same value as the StringValue of IdentifierName, return true.
    Return false.

12.2.6.4 静态语法:HasComputedPropertyKey

PropertyDefinitionList:PropertyDefinitionList,PropertyDefinition
    If HasComputedPropertyKey of PropertyDefinitionList is true, return true.
    Return HasComputedPropertyKey of PropertyDefinition.
PropertyDefinition:IdentifierReference
    Return false.
PropertyDefinition:PropertyName:AssignmentExpression
    Return IsComputedPropertyKey of PropertyName.

12.2.6.5 静态语法:IsComputedPropertyKey

PropertyName:LiteralPropertyName
    Return false.
PropertyName:ComputedPropertyName
    Return true.

12.2.6.6 静态语法:PropName

PropertyDefinition:IdentifierReference
    Return StringValue of IdentifierReference.
PropertyDefinition:PropertyName:AssignmentExpression
    Return PropName of PropertyName.
LiteralPropertyName:IdentifierName
    Return StringValue of IdentifierName.
LiteralPropertyName:StringLiteral
    Return a String value whose code units are the SV of the StringLiteral.
LiteralPropertyName:NumericLiteral
    Let nbr be the result of forming the value of the NumericLiteral.
    Return ! ToString(nbr).
ComputedPropertyName:[AssignmentExpression]
    Return empty.

12.2.6.7 静态语法:PropertyNameList

PropertyDefinitionList:PropertyDefinition
    If PropName of PropertyDefinition is empty, return a new empty List.
    Return a new List containing PropName of PropertyDefinition.

PropertyDefinitionList:PropertyDefinitionList,PropertyDefinition
    Let list be PropertyNameList of PropertyDefinitionList.
    If PropName of PropertyDefinition is empty, return list.
    Append PropName of PropertyDefinition to the end of list.
    Return list.

12.2.6.8 运行时语法:Evaluation

ObjectLiteral:{}
    Return ObjectCreate(%ObjectPrototype%).
    ObjectLiteral:
        {PropertyDefinitionList}
        {PropertyDefinitionList,}
    Let obj be ObjectCreate(%ObjectPrototype%).
    Let status be the result of performing PropertyDefinitionEvaluation of PropertyDefinitionList with arguments obj and true.
    ReturnIfAbrupt(status).
    Return obj.

LiteralPropertyName:IdentifierName
        Return StringValue of IdentifierName.

LiteralPropertyName:StringLiteral
    Return a String value whose code units are the SV of the StringLiteral.

LiteralPropertyName:NumericLiteral
    Let nbr be the result of forming the value of the NumericLiteral.
    Return ! ToString(nbr).

ComputedPropertyName:[AssignmentExpression]
    Let exprValue be the result of evaluating AssignmentExpression.
    Let propName be ? GetValue(exprValue).
    Return ? ToPropertyKey(propName).

12.2.6.9 运行时语法:PropertyDefinitionEvaluation

With parameters object and enumerable.

PropertyDefinitionList:PropertyDefinitionList,PropertyDefinition
    Let status be the result of performing PropertyDefinitionEvaluation of PropertyDefinitionList with arguments object and enumerable.
    ReturnIfAbrupt(status).
    Return the result of performing PropertyDefinitionEvaluation of PropertyDefinition with arguments object and enumerable.

PropertyDefinition:IdentifierReference
    Let propName be StringValue of IdentifierReference.
    Let exprValue be the result of evaluating IdentifierReference.
    Let propValue be ? GetValue(exprValue).
    Assert: enumerable is true.
    Return CreateDataPropertyOrThrow(object, propName, propValue).

PropertyDefinition:PropertyName:AssignmentExpression
    Let propKey be the result of evaluating PropertyName.
    ReturnIfAbrupt(propKey).
    Let exprValueRef be the result of evaluating AssignmentExpression.
    Let propValue be ? GetValue(exprValueRef).
    If IsAnonymousFunctionDefinition(AssignmentExpression) is true, then
    Let hasNameProperty be ? HasOwnProperty(propValue, "name").
    If hasNameProperty is false, perform SetFunctionName(propValue, propKey).
    Assert: enumerable is true.
    Return CreateDataPropertyOrThrow(object, propKey, propValue).
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 静态语法:早期错误

PrimaryExpression:RegularExpressionLiteral
    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.
    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

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

results matching ""

    No results matching ""