Annex B (normative) Web 浏览器的附加 ECMAScript 特性

当 ECMAScript 宿主是 Web 浏览器时,本附录中定义的 ECMAScript 语言语法和语义是必需的。如果 ECMAScript 宿主不是 Web 浏览器,则本附录内容是规范性的但可选。

本附录中定义的一些特性在本附录中规定,另一些则在本文档主体中规定。

当某个特性在主体中规定时,它影响文档的每一点都会以彩色框标记“Normative Optional”字样。此外,当该特性涉及算法或早期错误规则中的特定措辞时,会以“宿主支持”相关特性这一条件作为保护。Web 浏览器必须支持所有这些特性。

Note

本附录描述了 Web 浏览器 ECMAScript 宿主的各种遗留特性和其他特征。本附录中规定的所有语言特性和行为都具有一个或多个不理想的特征,如果没有遗留使用,本应从本规范中移除。然而,由于大量现有网页使用这些特性,Web 浏览器必须继续支持它们。本附录中的规范定义了这些遗留特性的可互操作实现要求。

这些特性不被认为是核心 ECMAScript 语言的一部分。程序员在编写新的 ECMAScript 代码时不应使用或假定这些特性和行为的存在。除非实现是 Web 浏览器的一部分,或需要运行 Web 浏览器所遇到的相同遗留 ECMAScript 代码,否则不鼓励 ECMAScript 实现实现这些特性。

B.1 附加语法

B.1.1 类 HTML 注释

12.4 的语法和语义扩展如下,但当使用目标符号 Module 解析源文本时,不允许此扩展:

语法

InputElementHashbangOrRegExp :: WhiteSpace LineTerminator Comment CommonToken HashbangComment RegularExpressionLiteral HTMLCloseComment Comment :: MultiLineComment SingleLineComment SingleLineHTMLOpenComment SingleLineHTMLCloseComment SingleLineDelimitedComment MultiLineComment :: /* FirstCommentLineopt LineTerminator MultiLineCommentCharsopt */ HTMLCloseCommentopt FirstCommentLine :: SingleLineDelimitedCommentChars SingleLineHTMLOpenComment :: <!-- SingleLineCommentCharsopt SingleLineHTMLCloseComment :: LineTerminatorSequence HTMLCloseComment SingleLineDelimitedComment :: /* SingleLineDelimitedCommentCharsopt */ HTMLCloseComment :: WhiteSpaceSequenceopt SingleLineDelimitedCommentSequenceopt --> SingleLineCommentCharsopt SingleLineDelimitedCommentChars :: SingleLineNotAsteriskChar SingleLineDelimitedCommentCharsopt * SingleLinePostAsteriskCommentCharsopt SingleLineNotAsteriskChar :: SourceCharacter but not one of * or LineTerminator SingleLinePostAsteriskCommentChars :: SingleLineNotForwardSlashOrAsteriskChar SingleLineDelimitedCommentCharsopt * SingleLinePostAsteriskCommentCharsopt SingleLineNotForwardSlashOrAsteriskChar :: SourceCharacter but not one of / or * or LineTerminator WhiteSpaceSequence :: WhiteSpace WhiteSpaceSequenceopt SingleLineDelimitedCommentSequence :: SingleLineDelimitedComment WhiteSpaceSequenceopt SingleLineDelimitedCommentSequenceopt

类似于包含行终止符码点的 MultiLineComment,为了按句法语法进行解析,SingleLineHTMLCloseComment 被视为 LineTerminator

B.1.2 正则表达式模式

22.2.1 的语法按如下方式修改和扩展。这些更改引入了歧义,这些歧义通过语法产生式的顺序和上下文信息来消解。使用以下语法进行解析时,只有在先前的产生式替代项不匹配时,才会考虑每个替代项。

此替代模式语法和语义只改变 BMP 模式的语法和语义。以下语法扩展包括带有 [UnicodeMode] 参数的产生式。然而,这些扩展均不改变在目标符号上存在 [UnicodeMode] 参数时解析所识别的 Unicode 模式语法。

语法

Term[UnicodeMode, UnicodeSetsMode, NamedCaptureGroups] :: [+UnicodeMode] Assertion[+UnicodeMode, ?UnicodeSetsMode, ?NamedCaptureGroups] [+UnicodeMode] Atom[+UnicodeMode, ?UnicodeSetsMode, ?NamedCaptureGroups] Quantifier [+UnicodeMode] Atom[+UnicodeMode, ?UnicodeSetsMode, ?NamedCaptureGroups] [~UnicodeMode] QuantifiableAssertion[?NamedCaptureGroups] Quantifier [~UnicodeMode] Assertion[~UnicodeMode, ~UnicodeSetsMode, ?NamedCaptureGroups] [~UnicodeMode] ExtendedAtom[?NamedCaptureGroups] Quantifier [~UnicodeMode] ExtendedAtom[?NamedCaptureGroups] Assertion[UnicodeMode, UnicodeSetsMode, NamedCaptureGroups] :: ^ $ \b \B [+UnicodeMode] (?= Disjunction[+UnicodeMode, ?UnicodeSetsMode, ?NamedCaptureGroups] ) [+UnicodeMode] (?! Disjunction[+UnicodeMode, ?UnicodeSetsMode, ?NamedCaptureGroups] ) [~UnicodeMode] QuantifiableAssertion[?NamedCaptureGroups] (?<= Disjunction[?UnicodeMode, ?UnicodeSetsMode, ?NamedCaptureGroups] ) (?<! Disjunction[?UnicodeMode, ?UnicodeSetsMode, ?NamedCaptureGroups] ) QuantifiableAssertion[NamedCaptureGroups] :: (?= Disjunction[~UnicodeMode, ~UnicodeSetsMode, ?NamedCaptureGroups] ) (?! Disjunction[~UnicodeMode, ~UnicodeSetsMode, ?NamedCaptureGroups] ) ExtendedAtom[NamedCaptureGroups] :: . \ AtomEscape[~UnicodeMode, ?NamedCaptureGroups] \ [lookahead = c] CharacterClass[~UnicodeMode, ~UnicodeSetsMode] ( GroupSpecifier[~UnicodeMode]opt Disjunction[~UnicodeMode, ~UnicodeSetsMode, ?NamedCaptureGroups] ) (? RegularExpressionModifiers : Disjunction[~UnicodeMode, ~UnicodeSetsMode, ?NamedCaptureGroups] ) (? RegularExpressionModifiers - RegularExpressionModifiers : Disjunction[~UnicodeMode, ~UnicodeSetsMode, ?NamedCaptureGroups] ) InvalidBracedQuantifier ExtendedPatternCharacter InvalidBracedQuantifier :: { DecimalDigits[~Sep] } { DecimalDigits[~Sep] ,} { DecimalDigits[~Sep] , DecimalDigits[~Sep] } ExtendedPatternCharacter :: SourceCharacter but not one of ^ $ \ . * + ? ( ) [ | AtomEscape[UnicodeMode, NamedCaptureGroups] :: [+UnicodeMode] DecimalEscape [~UnicodeMode] DecimalEscape but only if the CapturingGroupNumber of DecimalEscape is ≤ CountLeftCapturingParensWithin(the Pattern containing DecimalEscape) CharacterClassEscape[?UnicodeMode] CharacterEscape[?UnicodeMode, ?NamedCaptureGroups] [+NamedCaptureGroups] k GroupName[?UnicodeMode] CharacterEscape[UnicodeMode, NamedCaptureGroups] :: ControlEscape c AsciiLetter 0 [lookahead ∉ DecimalDigit] HexEscapeSequence RegExpUnicodeEscapeSequence[?UnicodeMode] [~UnicodeMode] LegacyOctalEscapeSequence IdentityEscape[?UnicodeMode, ?NamedCaptureGroups] IdentityEscape[UnicodeMode, NamedCaptureGroups] :: [+UnicodeMode] SyntaxCharacter [+UnicodeMode] / [~UnicodeMode] SourceCharacterIdentityEscape[?NamedCaptureGroups] SourceCharacterIdentityEscape[NamedCaptureGroups] :: [~NamedCaptureGroups] SourceCharacter but not c [+NamedCaptureGroups] SourceCharacter but not one of c or k ClassAtomNoDash[UnicodeMode, NamedCaptureGroups] :: SourceCharacter but not one of \ or ] or - \ ClassEscape[?UnicodeMode, ?NamedCaptureGroups] \ [lookahead = c] ClassEscape[UnicodeMode, NamedCaptureGroups] :: b [+UnicodeMode] - [~UnicodeMode] c ClassControlLetter CharacterClassEscape[?UnicodeMode] CharacterEscape[?UnicodeMode, ?NamedCaptureGroups] ClassControlLetter :: DecimalDigit _ Note

当相同的左侧同时以 [+UnicodeMode] 和 [~UnicodeMode] 保护出现时,这是为了控制消歧优先级。

B.1.2.1 静态语义:早期错误

22.2.1.1 的语义扩展如下:

ExtendedAtom :: InvalidBracedQuantifier
  • 如果任何源文本与此产生式匹配,则为语法错误。

此外,以下产生式的规则通过加入高亮文本进行修改:

NonemptyClassRanges :: ClassAtom - ClassAtom ClassContents NonemptyClassRangesNoDash :: ClassAtomNoDash - ClassAtom ClassContents

B.1.2.2 静态语义:CountLeftCapturingParensWithin 和 CountLeftCapturingParensBefore

CountLeftCapturingParensWithinCountLeftCapturingParensBefore 的定义中,对 “ Atom :: ( GroupSpecifieropt Disjunction ) ” 的引用应解释为表示 “ Atom :: ( GroupSpecifieropt Disjunction ) ” 或 “ ExtendedAtom :: ( GroupSpecifieropt Disjunction ) ”。

B.1.2.3 静态语义:IsCharacterClass

22.2.1.6 的语义扩展如下:

ClassAtomNoDash :: \ [lookahead = c]
  1. 返回 false

B.1.2.4 静态语义:CharacterValue

22.2.1.7 的语义扩展如下:

ClassAtomNoDash :: \ [lookahead = c]
  1. 返回 U+005C (REVERSE SOLIDUS) 的数值。
ClassEscape :: c ClassControlLetter
  1. ch 为与 ClassControlLetter 匹配的码点。
  2. ich 的数值。
  3. 返回 i 除以 32 的余数。
CharacterEscape :: LegacyOctalEscapeSequence
  1. 返回 LegacyOctalEscapeSequence 的 MV(见 12.9.4.3)。

B.1.2.5 运行时语义:CompileSubpattern

CompileSubpattern 的语义扩展如下:

Term :: QuantifiableAssertion Quantifier 的规则与 Term :: Atom Quantifier 的规则相同,但以 QuantifiableAssertion 替代 Atom

Term :: ExtendedAtom Quantifier 的规则与 Term :: Atom Quantifier 的规则相同,但以 ExtendedAtom 替代 Atom

Term :: ExtendedAtom 的规则与 Term :: Atom 的规则相同,但以 ExtendedAtom 替代 Atom

B.1.2.6 运行时语义:CompileAssertion

Assertion :: (?= Disjunction ) Assertion :: (?! Disjunction ) 产生式的 CompileAssertion 规则也用于 QuantifiableAssertion 产生式,但以 QuantifiableAssertion 替代 Assertion

B.1.2.7 运行时语义:CompileAtom

Atom :: PatternCharacter 外,Atom 产生式的 CompileAtom 规则也用于 ExtendedAtom 产生式,但以 ExtendedAtom 替代 Atom。还添加以下带参数 direction 的规则:

ExtendedAtom :: \ [lookahead = c]
  1. charSet 为包含单个字符 \ U+005C (REVERSE SOLIDUS) 的 CharSet
  2. 返回 CharacterSetMatcher(regexpRecord, charSet, false, direction)。
ExtendedAtom :: ExtendedPatternCharacter
  1. chExtendedPatternCharacter 所表示的字符。
  2. charSet 为包含字符 ch 的单元素 CharSet
  3. 返回 CharacterSetMatcher(regexpRecord, charSet, false, direction)。

B.1.2.8 运行时语义:CompileToCharSet

22.2.2.9 的语义扩展如下:

以下两条规则替换 CompileToCharSet 的对应规则。

NonemptyClassRanges :: ClassAtom - ClassAtom ClassContents
  1. charSet 为第一个 ClassAtomregexpRecord 为实参的 CompileToCharSet
  2. otherSet 为第二个 ClassAtomregexpRecord 为实参的 CompileToCharSet
  3. remainingSetClassContentsregexpRecord 为实参的 CompileToCharSet
  4. rangeSetCharacterRangeOrUnion(regexpRecord, charSet, otherSet)。
  5. 返回 rangeSetremainingSet 的并集。
NonemptyClassRangesNoDash :: ClassAtomNoDash - ClassAtom ClassContents
  1. charSetClassAtomNoDashregexpRecord 为实参的 CompileToCharSet
  2. otherSetClassAtomregexpRecord 为实参的 CompileToCharSet
  3. remainingSetClassContentsregexpRecord 为实参的 CompileToCharSet
  4. rangeSetCharacterRangeOrUnion(regexpRecord, charSet, otherSet)。
  5. 返回 rangeSetremainingSet 的并集。

此外,向 CompileToCharSet 添加以下规则。

ClassEscape :: c ClassControlLetter
  1. cv 为此 ClassEscapeCharacterValue
  2. c 为字符值为 cv 的字符。
  3. 返回包含单个字符 cCharSet
ClassAtomNoDash :: \ [lookahead = c]
  1. 返回包含单个字符 \ U+005C (REVERSE SOLIDUS) 的 CharSet
Note
此产生式只能从字符类中的序列 \c 到达,且其后未跟随可接受的控制字符。

B.1.2.8.1 CharacterRangeOrUnion ( regexpRecord, charSet, otherSet )

The abstract operation CharacterRangeOrUnion takes arguments regexpRecord (一个 RegExp Record), charSet (一个 CharSet), and otherSet (一个 CharSet) and returns 一个 CharSet. It performs the following steps when called:

  1. 如果 HasEitherUnicodeFlag(regexpRecord) 为 false,则
    1. 如果 charSet 不恰好包含一个字符,或 otherSet 不恰好包含一个字符,则
      1. remainingSet 为包含单个字符 - U+002D (HYPHEN-MINUS) 的 CharSet
      2. 返回 CharSet charSetotherSetremainingSet 的并集。
  2. 返回 CharacterRange(charSet, otherSet)。

B.1.2.9 静态语义:ParsePattern ( patternText, u, v )

22.2.3.4 的语义扩展如下:

抽象操作 ParsePattern 接受实参 patternText(一个 Unicode 码点序列)、u(一个 Boolean)和 v(一个 Boolean)。它在被调用时执行以下步骤:

  1. 如果 vtrueutrue,则
    1. parseResult 为包含一个或多个 SyntaxError 对象的 List
  2. 否则,如果 vtrue,则
    1. parseResultParseText(patternText, Pattern[+UnicodeMode, +UnicodeSetsMode, +NamedCaptureGroups])。
  3. 否则,如果 utrue,则
    1. parseResultParseText(patternText, Pattern[+UnicodeMode, ~UnicodeSetsMode, +NamedCaptureGroups])。
  4. 否则,
    1. parseResultParseText(patternText, Pattern[~UnicodeMode, ~UnicodeSetsMode, ~NamedCaptureGroups])。
    2. 如果 parseResult 是 Parse Node 且 parseResult 包含 GroupName,则
      1. parseResult 设为 ParseText(patternText, Pattern[~UnicodeMode, ~UnicodeSetsMode, +NamedCaptureGroups])。
  5. 返回 parseResult

B.2 附加内置属性

当 ECMAScript 宿主是 Web 浏览器时,定义标准内置对象的以下附加属性。

B.2.1 全局对象的附加属性

Table 98 中的条目被添加到 Table 6

Table 98: 附加知名内在对象
内在名称 全局名称 ECMAScript 语言关联
%escape% escape escape 函数(B.2.1.1
%unescape% unescape unescape 函数(B.2.1.2

B.2.1.1 escape ( string )

此函数是全局对象的属性。它计算一个 String 值的新版本,其中某些代码单元已被十六进制转义序列替换。

当替换数值小于或等于 0x00FF 的代码单元时,使用形如 %xx 的两位转义序列。当替换数值严格大于 0x00FF 的代码单元时,使用形如 %uxxxx 的四位转义序列。

它是 %escape% 内在对象。

它在被调用时执行以下步骤:

  1. string 设为 ? ToString(string)。
  2. lenstring 的长度。
  3. result 为空字符串。
  4. unescapedSet 为 ASCII word characters 与 "@*+-./" 的字符串拼接。
  5. k 为 0。
  6. 重复,当 k < len 时,
    1. codeUnitstring 中索引 k 处的代码单元。
    2. 如果 unescapedSet 包含 codeUnit,则
      1. strcodeUnit
    3. 否则,
      1. ncodeUnit 的数值。
      2. 如果 n < 256,则
        1. hexn 的 String 表示,格式化为大写十六进制数。
        2. str"%"StringPad(hex, 2, "0", start) 的字符串拼接。
      3. 否则,
        1. hexn 的 String 表示,格式化为大写十六进制数。
        2. str"%u"StringPad(hex, 4, "0", start) 的字符串拼接。
    4. result 设为 resultstr 的字符串拼接。
    5. k 设为 k + 1。
  7. 返回 result
Note

该编码部分基于 RFC 1738 中描述的编码,但本标准中指定的整个编码已在上文描述,而不考虑 RFC 1738 的内容。该编码不反映 RFC 3986 对 RFC 1738 所作的更改。

B.2.1.2 unescape ( string )

此函数是全局对象的属性。它计算一个 String 值的新版本,其中每个可能由 escape 函数引入的那类转义序列都被替换为其表示的代码单元。

它是 %unescape% 内在对象。

它在被调用时执行以下步骤:

  1. string 设为 ? ToString(string)。
  2. lenstring 的长度。
  3. result 为空字符串。
  4. k 为 0。
  5. 重复,当 k < len 时,
    1. codeUnitstring 中索引 k 处的代码单元。
    2. 如果 codeUnit 是代码单元 0x0025 (PERCENT SIGN),则
      1. hexDigits 为空字符串。
      2. optionalAdvance 为 0。
      3. 如果 k + 5 < lenstring 中索引 k + 1 处的代码单元是代码单元 0x0075 (LATIN SMALL LETTER U),则
        1. hexDigits 设为 stringk + 2 到 k + 6 的子字符串
        2. optionalAdvance 设为 5。
      4. 否则,如果 k + 3 ≤ len,则
        1. hexDigits 设为 stringk + 1 到 k + 3 的子字符串
        2. optionalAdvance 设为 2。
      5. parseResultParseText(hexDigits, HexDigits[~Sep])。
      6. 如果 parseResult 是 Parse Node,则
        1. nparseResult 的 MV。
        2. codeUnit 设为数值为 n 的代码单元。
        3. k 设为 k + optionalAdvance
    3. result 设为 resultcodeUnit 的字符串拼接。
    4. k 设为 k + 1。
  6. 返回 result

B.2.2 String.prototype 对象的附加属性

B.2.2.1 String.prototype.substr ( start, length )

此方法返回将 this 值转换为 String 后所得结果的一个子字符串,从索引 start 开始并持续 length 个代码单元(如果 lengthundefined,则直到 String 末尾)。如果 start 为负,则将其视为 sourceLength + start,其中 sourceLength 是该 String 的长度。结果是一个 String 值,而不是 String 对象。

它在被调用时执行以下步骤:

  1. objthis 值。
  2. 执行 ? RequireObjectCoercible(obj)。
  3. str 为 ? ToString(obj)。
  4. sizestr 的长度。
  5. intStart 为 ? ToIntegerOrInfinity(start)。
  6. 如果 intStart = -∞,将 intStart 设为 0。
  7. 否则,如果 intStart < 0,将 intStart 设为 max(size + intStart, 0)。
  8. 否则,将 intStart 设为 min(intStart, size)。
  9. 如果 lengthundefined,令 intLengthsize;否则令 intLength 为 ? ToIntegerOrInfinity(length)。
  10. intLength 设为把 intLength 夹在 0 和 size 之间的结果。
  11. intEndmin(intStart + intLength, size)。
  12. 返回 strintStartintEnd子字符串
Note

此方法有意设计为泛型;它不要求其 this 值为 String 对象。因此它可以转移到其他种类的对象上作为方法使用。

B.2.2.2 String.prototype.anchor ( name )

此方法在被调用时执行以下步骤:

  1. strthis 值。
  2. 返回 ? CreateHTML(str, "a", "name", name)。

B.2.2.2.1 CreateHTML ( contents, tag, attribute, attrValue )

The abstract operation CreateHTML takes arguments contents (一个 ECMAScript 语言值), tag (一个 String), attribute (一个 String), and attrValue (一个 ECMAScript 语言值) and returns 一个包含 String 的 normal completion 或一个 throw completion. It performs the following steps when called:

  1. 执行 ? RequireObjectCoercible(contents)。
  2. contentsStr 为 ? ToString(contents)。
  3. p1"<"tag 的字符串拼接。
  4. 如果 attribute 不是空字符串,则
    1. attrValueStr 为 ? ToString(attrValue)。
    2. escapedAttrValue 为一个 String 值,它与 attrValueStr 相同,但 attrValueStr 中每次出现的代码单元 0x0022 (QUOTATION MARK) 都被替换为六代码单元序列 "&quot;"
    3. p1 设为以下项的字符串拼接:
      • p1
      • 代码单元 0x0020 (SPACE)
      • attribute
      • 代码单元 0x003D (EQUALS SIGN)
      • 代码单元 0x0022 (QUOTATION MARK)
      • escapedAttrValue
      • 代码单元 0x0022 (QUOTATION MARK)
  5. p2p1">" 的字符串拼接。
  6. p3p2contentsStr 的字符串拼接。
  7. p4p3"</"tag">" 的字符串拼接。
  8. 返回 p4

B.2.2.3 String.prototype.big ( )

此方法在被调用时执行以下步骤:

  1. strthis 值。
  2. 返回 ? CreateHTML(str, "big", "", "")。

B.2.2.4 String.prototype.blink ( )

此方法在被调用时执行以下步骤:

  1. strthis 值。
  2. 返回 ? CreateHTML(str, "blink", "", "")。

B.2.2.5 String.prototype.bold ( )

此方法在被调用时执行以下步骤:

  1. strthis 值。
  2. 返回 ? CreateHTML(str, "b", "", "")。

B.2.2.6 String.prototype.fixed ( )

此方法在被调用时执行以下步骤:

  1. strthis 值。
  2. 返回 ? CreateHTML(str, "tt", "", "")。

B.2.2.7 String.prototype.fontcolor ( colour )

此方法在被调用时执行以下步骤:

  1. strthis 值。
  2. 返回 ? CreateHTML(str, "font", "color", colour)。

B.2.2.8 String.prototype.fontsize ( size )

此方法在被调用时执行以下步骤:

  1. strthis 值。
  2. 返回 ? CreateHTML(str, "font", "size", size)。

B.2.2.9 String.prototype.italics ( )

此方法在被调用时执行以下步骤:

  1. strthis 值。
  2. 返回 ? CreateHTML(str, "i", "", "")。

B.2.2.10 String.prototype.link ( url )

此方法在被调用时执行以下步骤:

  1. strthis 值。
  2. 返回 ? CreateHTML(str, "a", "href", url)。

B.2.2.11 String.prototype.small ( )

此方法在被调用时执行以下步骤:

  1. strthis 值。
  2. 返回 ? CreateHTML(str, "small", "", "")。

B.2.2.12 String.prototype.strike ( )

此方法在被调用时执行以下步骤:

  1. strthis 值。
  2. 返回 ? CreateHTML(str, "strike", "", "")。

B.2.2.13 String.prototype.sub ( )

此方法在被调用时执行以下步骤:

  1. strthis 值。
  2. 返回 ? CreateHTML(str, "sub", "", "")。

B.2.2.14 String.prototype.sup ( )

此方法在被调用时执行以下步骤:

  1. strthis 值。
  2. 返回 ? CreateHTML(str, "sup", "", "")。

B.2.2.15 String.prototype.trimLeft ( )

Note

属性 "trimStart" 是首选。"trimLeft" 属性主要为兼容旧代码而提供。建议在新的 ECMAScript 代码中使用 "trimStart" 属性。

"trimLeft" 属性的初始值是 %String.prototype.trimStart%,定义于 22.1.3.34

B.2.2.16 String.prototype.trimRight ( )

Note

属性 "trimEnd" 是首选。"trimRight" 属性主要为兼容旧代码而提供。建议在新的 ECMAScript 代码中使用 "trimEnd" 属性。

"trimRight" 属性的初始值是 %String.prototype.trimEnd%,定义于 22.1.3.33

B.2.3 Date.prototype 对象的附加属性

B.2.3.1 Date.prototype.getYear ( )

Note

几乎所有用途都首选 getFullYear 方法,因为它避免了“2000 年问题”。

此方法在被调用时执行以下步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. 如果 tNaN,返回 NaN
  5. 返回 YearFromTime(LocalTime(t)) - 1900𝔽

B.2.3.2 Date.prototype.setYear ( year )

Note

几乎所有用途都首选 setFullYear 方法,因为它避免了“2000 年问题”。

此方法在被调用时执行以下步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. timedateObject.[[DateValue]]
  4. year 为 ? ToNumber(year)。
  5. 如果 timeNaN,将 time 设为 +0𝔽;否则将 time 设为 LocalTime(time)。
  6. fullYearMakeFullYear(year)。
  7. dayMakeDay(fullYear, MonthFromTime(time), DateFromTime(time))。
  8. dateMakeDate(day, TimeWithinDay(time))。
  9. utcTimestampTimeClip(UTC(date))。
  10. dateObject.[[DateValue]] 设为 utcTimestamp
  11. 返回 utcTimestamp

B.2.3.3 Date.prototype.toGMTString ( )

Note

toUTCString 方法是首选。此方法主要为兼容旧代码而提供。

"toGMTString" 属性的初始值是 %Date.prototype.toUTCString%,定义于 21.4.4.43

B.2.4 RegExp.prototype 对象的附加属性

B.2.4.1 RegExp.prototype.compile ( pattern, flags )

此方法在被调用时执行以下步骤:

  1. objthis 值。
  2. 执行 ? RequireInternalSlot(obj, [[RegExpMatcher]])。
  3. 如果 pattern 是 Object 且 pattern 具有 [[RegExpMatcher]] 内部槽,则
    1. 如果 flags 不是 undefined,抛出 TypeError 异常。
    2. flags 设为 pattern.[[OriginalFlags]]
    3. pattern 设为 pattern.[[OriginalSource]]
  4. 返回 ? RegExpInitialize(obj, pattern, flags)。
Note

此方法会用新的模式和标志完全重新初始化 this 值 RegExp。实现可以将对此方法的使用解释为一种断言,即所得 RegExp 对象将被多次使用,因此是额外优化的候选对象。

B.3 其他附加特性

B.3.1 带标签的函数声明

在 ECMAScript 2015 之前,LabelledStatement 的规范不允许将语句标签与 FunctionDeclaration 关联。然而,带标签的 FunctionDeclaration非严格代码中允许的扩展,并且大多数浏览器托管的 ECMAScript 实现都支持该扩展。在 ECMAScript 2015 及以后版本中,LabelledStatement 的语法产生式允许将 FunctionDeclaration 用作 LabelledItem,但 14.13.1 包含一个 Early Error 规则,如果发生这种情况会产生语法错误。随后修改该规则,以便在宿主支持此特性时,在非严格代码中抑制该语法错误。

Note

WithStatementIfStatementIterationStatement早期错误规则会阻止这些语句在非严格代码中包含带标签的 FunctionDeclaration

B.3.2 块级函数声明的 Web 遗留兼容性语义

在 ECMAScript 2015 之前,ECMAScript 规范没有定义 FunctionDeclaration 作为 Block 语句的 StatementList 元素出现的情况。然而,对这种 FunctionDeclaration 形式的支持是允许的扩展,并且大多数浏览器托管的 ECMAScript 实现都允许它们。不幸的是,这类声明的语义在这些实现之间有所不同。由于这些语义差异,使用 Block 级函数声明的现有 Web ECMAScript 源文本,只有当其用法仅依赖所有浏览器实现对此类声明的语义交集时,才可在浏览器实现之间移植。以下是落入该交集语义范围内的用例:

  1. 函数被声明且仅在单个块内被引用。

  2. 函数被声明且可能在单个 Block 内使用,但也被不包含在该同一 Block 内的内部函数定义引用。

  3. 函数被声明且可能在单个块内使用,但也在后续块中被引用。

第一个用例可与 ECMAScript 2015 提供的 Block 级函数声明语义互操作。任何采用该用例的既有 ECMAScript 源文本都将使用条款 101415 所定义的块级函数声明语义运行。

第二和第三个用例的 ECMAScript 2015 互操作性需要对条款 10、条款 15、条款 19.2.1 和条款 16.1.7 的语义进行以下扩展。

如果 ECMAScript 实现具有报告诊断警告消息的机制,则当代码包含适用了这些兼容性语义且引入了与非兼容性语义可观察差异的 FunctionDeclaration 时,应产生警告。例如,如果未引入 var 绑定是因为其引入会造成早期错误,则不应产生警告消息。

此特性在以下位置涉及特殊语义:

B.3.3 IfStatement 语句子句中的 FunctionDeclaration

以下内容扩充了 14.6 中的 IfStatement 产生式:

IfStatement[Yield, Await, Return] : if ( Expression[+In, ?Yield, ?Await] ) FunctionDeclaration[?Yield, ?Await, ~Default] else Statement[?Yield, ?Await, ?Return] if ( Expression[+In, ?Yield, ?Await] ) Statement[?Yield, ?Await, ?Return] else FunctionDeclaration[?Yield, ?Await, ~Default] if ( Expression[+In, ?Yield, ?Await] ) FunctionDeclaration[?Yield, ?Await, ~Default] else FunctionDeclaration[?Yield, ?Await, ~Default] if ( Expression[+In, ?Yield, ?Await] ) FunctionDeclaration[?Yield, ?Await, ~Default] [lookahead ≠ else]

此产生式仅在解析非严格代码时适用。与此产生式匹配的源文本会被处理为:每个匹配出现的 FunctionDeclaration[?Yield, ?Await, ~Default] 都是一个占据源文本中该位置的 BlockStatement 的唯一 StatementListItem。此类合成 BlockStatement 的语义包括 B.3.2 中规定的 Web 遗留兼容性语义。

B.3.4 Catch 块中的 VariableStatement

在此特性中,Catch 子句的 Block 可以包含绑定某个名称的 var 声明,而该名称也由 CatchParameter 绑定。这是通过修改 14.15.1 Catch : catch ( CatchParameter ) Block 的一个早期错误规则来实现的。

Note

运行时,此类绑定在 VariableDeclarationEnvironment 中实例化。它们不会遮蔽由 CatchParameter 引入的同名绑定,因此此类 var 声明的 Initializer 将赋值给对应的 catch 参数,而不是 var 绑定。

此修改后的行为也适用于由包含在 Catch 子句的 Block 内的直接 eval 调用引入的 varfunction 声明。此更改通过修改 EvalDeclarationInstantiation 中的步骤 3.d.i.2.a.i13.b.ii.4.a.i.i 来实现。

B.3.5 ForIn 语句头中的初始化器

以下内容扩充了 14.7.5 中的 ForInOfStatement 产生式:

ForInOfStatement[Yield, Await, Return] : for ( var BindingIdentifier[?Yield, ?Await] Initializer[~In, ?Yield, ?Await] in Expression[+In, ?Yield, ?Await] ) Statement[?Yield, ?Await, ?Return]

此产生式仅在解析非严格代码时适用。

8.3.1ContainsDuplicateLabels静态语义扩充如下:

ForInOfStatement : for ( var BindingIdentifier Initializer in Expression ) Statement
  1. 返回 StatementlabelSet 为实参的 ContainsDuplicateLabels

8.3.2ContainsUndefinedBreakTarget静态语义扩充如下:

ForInOfStatement : for ( var BindingIdentifier Initializer in Expression ) Statement
  1. 返回 StatementlabelSet 为实参的 ContainsUndefinedBreakTarget

8.3.3ContainsUndefinedContinueTarget静态语义扩充如下:

ForInOfStatement : for ( var BindingIdentifier Initializer in Expression ) Statement
  1. 返回 StatementiterationSet 和 « » 为实参的 ContainsUndefinedContinueTarget

14.7.5.2IsDestructuring静态语义扩充如下:

BindingIdentifier : Identifier yield await
  1. 返回 false

8.2.6VarDeclaredNames静态语义扩充如下:

ForInOfStatement : for ( var BindingIdentifier Initializer in Expression ) Statement
  1. names1BindingIdentifierBoundNames
  2. names2StatementVarDeclaredNames
  3. 返回 names1names2列表连接

8.2.7VarScopedDeclarations静态语义扩充如下:

ForInOfStatement : for ( var BindingIdentifier Initializer in Expression ) Statement
  1. declarations1 为 « BindingIdentifier »。
  2. declarations2StatementVarScopedDeclarations
  3. 返回 declarations1declarations2列表连接

14.7.5.5ForInOfLoopEvaluation运行时语义扩充如下:

ForInOfStatement : for ( var BindingIdentifier Initializer in Expression ) Statement
  1. bindingIdBindingIdentifierStringValue
  2. lhs 为 ? ResolveBinding(bindingId)。
  3. 如果 IsAnonymousFunctionDefinition(Initializer) 为 true,则
    1. valueInitializerbindingId 为实参的 NamedEvaluation
  4. 否则,
    1. rhs 为 ? 对 Initializer 求值。
    2. value 为 ? GetValue(rhs)。
  5. 执行 ? PutValue(lhs, value)。
  6. keyResult 为 ? ForIn/OfHeadEvaluation(« », Expression, enumerate)。
  7. 返回 ? ForIn/OfBodyEvaluation(BindingIdentifier, Statement, keyResult, enumerate, var-binding, labelSet)。

B.3.6 [[IsHTMLDDA]] 内部槽

[[IsHTMLDDA]] 内部槽可以存在于宿主定义对象上。具有 [[IsHTMLDDA]] 内部槽的对象在 ToBooleanIsLooselyEqual 抽象操作中,以及用作 typeof 运算符的操作数时,表现得像 undefined

Note

具有 [[IsHTMLDDA]] 内部槽的对象从不会由本规范创建。然而,Web 浏览器中的 document.all 对象是一个具有此槽的宿主定义异质对象,它为 Web 兼容性目的而存在。除此 document.all 外,没有其他已知的此类对象示例,实现不应创建任何此类对象。

此特性在以下位置涉及特殊语义:

B.3.7 HostMakeJobCallback 中的非默认行为

HostMakeJobCallback 抽象操作允许作为 Web 浏览器的宿主指定非默认行为。

B.3.8 HostEnsureCanAddPrivateElement 中的非默认行为

HostEnsureCanAddPrivateElement 抽象操作允许作为 Web 浏览器的宿主指定非默认行为。

B.3.9 函数调用作为赋值目标时的运行时错误

当函数调用(13.3.6)在非严格代码中用作赋值目标时,不会产生早期错误,而是在赋值求值期间抛出 ReferenceError 异常。

Note

当赋值目标是 AssignmentExpressionLeftHandSideExpression 时,赋值运算符必须是 =AssignmentOperator;特别地,此处的允许并不适用于逻辑赋值运算符(??=&&=||=)。

AssignmentTargetType 中针对 CallExpression : CoverCallExpressionAndAsyncArrowHead CallExpression : CallExpression Arguments 的步骤 1