Annex B (normative) 供网页浏览器使用的额外 ECMAScript 特性

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

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

当某个特性在正文中定义时,每个其影响文档的点都会用带颜色框中的 “Normative Optional” 字样标记。此外,如果该特性牵涉算法或早期错误规则中的特定措辞,则这些措辞会以 “the host supports” 该相关特性为条件加以保护。网页浏览器被要求支持所有此类特性。

Note

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

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

B.1 附加语法

B.1.1 类 HTML 注释

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

Syntax

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

类似包含行终止符代码点的 MultiLineCommentSingleLineHTMLCloseComment 在句法语法解析目的上被视为一个 LineTerminator

B.1.2 正则表达式模式

22.2.1 的语法按如下方式修改与扩展。这些更改引入了通过文法产生式的排序与上下文信息消解的歧义。使用下列语法解析时,只有此前的产生式候选不匹配时才考虑后续候选。

这种替代模式文法与语义只改变 BMP 模式的语法与语义。下面的语法扩展包含带有 [UnicodeMode] 参数的产生式。然而,这些扩展均不改变在目标符号上存在 [UnicodeMode] 参数时被识别的 Unicode 模式的语法。

Syntax

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
  • 若第一个 ClassAtom 的 IsCharacterClass 为 true 或第二个 ClassAtom 的 IsCharacterClass 为 true 且该产生式具有 [UnicodeMode] 参数,则为语法错误。
  • 若第一个 ClassAtom 的 IsCharacterClass 为 false,第二个 ClassAtom 的 IsCharacterClass 为 false,且第一个 ClassAtom 的 CharacterValue 严格大于第二个的 CharacterValue,则为语法错误。
NonemptyClassRangesNoDash :: ClassAtomNoDash - ClassAtom ClassContents
  • ClassAtomNoDash 的 IsCharacterClass 为 trueClassAtom 的 IsCharacterClass 为 true 且该产生式具有 [UnicodeMode] 参数,则为语法错误。
  • ClassAtomNoDash 的 IsCharacterClass 为 falseClassAtom 的 IsCharacterClass 为 false,且 ClassAtomNoDash 的 CharacterValue 严格大于 ClassAtom 的 CharacterValue,则为语法错误。

B.1.2.2 静态语义:CountLeftCapturingParensWithin 与 CountLeftCapturingParensBefore

在 CountLeftCapturingParensWithin 与 CountLeftCapturingParensBefore 的定义中,对 “ 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. chClassControlLetter 匹配的代码点。
  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. A 为包含单个字符 \ U+005C (REVERSE SOLIDUS) 的 CharSet
  2. 返回 CharacterSetMatcher(rer, A, false, direction)。
ExtendedAtom :: ExtendedPatternCharacter
  1. chExtendedPatternCharacter 所表示的字符。
  2. A 为包含字符 ch 的单元素 CharSet
  3. 返回 CharacterSetMatcher(rer, A, false, direction)。

B.1.2.8 运行时语义:CompileToCharSet

22.2.2.9 的语义按如下扩展:

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

NonemptyClassRanges :: ClassAtom - ClassAtom ClassContents
  1. A 为第一个 ClassAtom 的 CompileToCharSet,参数 rer
  2. B 为第二个 ClassAtom 的 CompileToCharSet,参数 rer
  3. CClassContents 的 CompileToCharSet,参数 rer
  4. DCharacterRangeOrUnion(rer, A, B)。
  5. 返回 DC 的并集。
NonemptyClassRangesNoDash :: ClassAtomNoDash - ClassAtom ClassContents
  1. AClassAtomNoDash 的 CompileToCharSet,参数 rer
  2. BClassAtom 的 CompileToCharSet,参数 rer
  3. CClassContents 的 CompileToCharSet,参数 rer
  4. DCharacterRangeOrUnion(rer, A, B)。
  5. 返回 DC 的并集。

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

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

B.1.2.8.1 CharacterRangeOrUnion ( rer, A, B )

The abstract operation CharacterRangeOrUnion takes arguments rer (a RegExp Record), A (a CharSet), and B (a CharSet) and returns a CharSet. It performs the following steps when called:

  1. HasEitherUnicodeFlag(rer) 为 false,则
    1. A 不正好包含一个字符或 B 不正好包含一个字符,则
      1. C 为包含单个字符 - U+002D (HYPHEN-MINUS) 的 CharSet
      2. 返回 CharSet ABC 的并集。
  2. 返回 CharacterRange(A, B)。

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. parseResult 为 ParseText(patternText, Pattern[+UnicodeMode, +UnicodeSetsMode, +NamedCaptureGroups])。
  3. 否则若 utrue,则
    1. parseResult 为 ParseText(patternText, Pattern[+UnicodeMode, ~UnicodeSetsMode, +NamedCaptureGroups])。
  4. 否则,
    1. parseResult 为 ParseText(patternText, Pattern[~UnicodeMode, ~UnicodeSetsMode, ~NamedCaptureGroups])。
    2. parseResult 是 Parse Node 且 parseResult 包含 GroupName,则
      1. parseResult 设为 ParseText(patternText, Pattern[~UnicodeMode, ~UnicodeSetsMode, +NamedCaptureGroups])。
  5. 返回 parseResult

B.2 额外内建属性

当 ECMAScript 宿主是网页浏览器时,标准内建对象具有以下额外属性。

B.2.1 全局对象的额外属性

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

Table 100: 额外的著名内在对象
内在名称 全局名称 ECMAScript 语言关联
%escape% escape escape 函数 (B.2.1.1)
%unescape% unescape unescape 函数 (B.2.1.2)

B.2.1.1 escape ( string )

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

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

它是 %escape% 内在对象。

调用时执行以下步骤:

  1. string 设为 ? ToString(string)。
  2. lenstring 的长度。
  3. R 为空字符串。
  4. unescapedSet 为 ASCII 单词字符与 "@*+-./" 的串联。
  5. k 为 0。
  6. k < len 重复,
    1. Cstring 中索引 k 处的代码单元。
    2. unescapedSet 包含 C,则
      1. SC
    3. 否则,
      1. nC 的数值。
      2. n < 256,则
        1. hex 为将 n 格式化为大写十六进制数的字符串表示。
        2. S"%"StringPad(hex, 2, "0", start) 的串联。
      3. 否则,
        1. hex 为将 n 格式化为大写十六进制数的字符串表示。
        2. S"%u"StringPad(hex, 4, "0", start) 的串联。
    4. R 设为 RS 的串联。
    5. k 设为 k + 1。
  7. 返回 R
Note

该编码部分基于 RFC 1738 描述的编码,但本标准指定的全部编码如上所述,与 RFC 1738 内容无关。该编码不反映 RFC 3986 对 RFC 1738 的更改。

B.2.1.2 unescape ( string )

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

它是 %unescape% 内在对象。

调用时执行以下步骤:

  1. string 设为 ? ToString(string)。
  2. lenstring 的长度。
  3. R 为空字符串。
  4. k 为 0。
  5. k < len 重复,
    1. Cstring 中索引 k 处的代码单元。
    2. C 是代码单元 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. parseResult 为 ParseText(hexDigits, HexDigits[~Sep])。
      6. parseResult 是 Parse Node,则
        1. nparseResult 的 MV。
        2. C 设为数值为 n 的代码单元。
        3. k 设为 k + optionalAdvance
    3. R 设为 RC 的串联。
    4. k 设为 k + 1。
  6. 返回 R

B.2.2 String.prototype 对象的额外属性

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

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

调用时执行以下步骤:

  1. Othis 值。
  2. 执行 ? RequireObjectCoercible(O)。
  3. S 为 ? ToString(O)。
  4. sizeS 的长度。
  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 夹取到 0 与 size 之间。
  11. intEndmin(intStart + intLength, size)。
  12. 返回 SintStartintEnd 的子串。
Note

该方法有意设计为泛型;它不要求其 this 值为字符串对象。因此可以被转移到其他对象上用作方法。

B.2.2.2 String.prototype.anchor ( name )

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

  1. Sthis 值。
  2. 返回 ? CreateHTML(S, "a", "name", name)。

B.2.2.2.1 CreateHTML ( string, tag, attribute, value )

The abstract operation CreateHTML takes arguments string (an ECMAScript language value), tag (a String), attribute (a String), and value (an ECMAScript language value) and returns either a normal completion containing a String or a throw completion. It performs the following steps when called:

  1. 执行 ? RequireObjectCoercible(string)。
  2. S 为 ? ToString(string)。
  3. p1"<"tag 的串联。
  4. attribute 不是空字符串,则
    1. V 为 ? ToString(value)。
    2. escapedV 为与 V 相同的字符串值,除了 V 中每个代码单元 0x0022 (QUOTATION MARK) 被六个代码单元序列 "&quot;" 替换。
    3. p1 设为以下串联:
      • p1
      • 代码单元 0x0020 (SPACE)
      • attribute
      • 代码单元 0x003D (EQUALS SIGN)
      • 代码单元 0x0022 (QUOTATION MARK)
      • escapedV
      • 代码单元 0x0022 (QUOTATION MARK)
  5. p2p1">" 的串联。
  6. p3p2S 的串联。
  7. p4p3"</"tag">" 的串联。
  8. 返回 p4

B.2.2.3 String.prototype.big ( )

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

  1. Sthis 值。
  2. 返回 ? CreateHTML(S, "big", "", "")。

B.2.2.4 String.prototype.blink ( )

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

  1. Sthis 值。
  2. 返回 ? CreateHTML(S, "blink", "", "")。

B.2.2.5 String.prototype.bold ( )

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

  1. Sthis 值。
  2. 返回 ? CreateHTML(S, "b", "", "")。

B.2.2.6 String.prototype.fixed ( )

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

  1. Sthis 值。
  2. 返回 ? CreateHTML(S, "tt", "", "")。

B.2.2.7 String.prototype.fontcolor ( colour )

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

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

B.2.2.8 String.prototype.fontsize ( size )

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

  1. Sthis 值。
  2. 返回 ? CreateHTML(S, "font", "size", size)。

B.2.2.9 String.prototype.italics ( )

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

  1. Sthis 值。
  2. 返回 ? CreateHTML(S, "i", "", "")。

B.2.2.10 String.prototype.link ( url )

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

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

B.2.2.11 String.prototype.small ( )

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

  1. Sthis 值。
  2. 返回 ? CreateHTML(S, "small", "", "")。

B.2.2.12 String.prototype.strike ( )

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

  1. Sthis 值。
  2. 返回 ? CreateHTML(S, "strike", "", "")。

B.2.2.13 String.prototype.sub ( )

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

  1. Sthis 值。
  2. 返回 ? CreateHTML(S, "sub", "", "")。

B.2.2.14 String.prototype.sup ( )

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

  1. Sthis 值。
  2. 返回 ? CreateHTML(S, "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. tdateObject.[[DateValue]]
  4. y 为 ? ToNumber(year)。
  5. tNaN,将 t 设为 +0𝔽;否则将 t 设为 LocalTime(t)。
  6. yyyyMakeFullYear(y)。
  7. dMakeDay(yyyy, MonthFromTime(t), DateFromTime(t))。
  8. dateMakeDate(d, TimeWithinDay(t))。
  9. uTimeClip(UTC(date))。
  10. dateObject.[[DateValue]] 设为 u
  11. 返回 u

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. Othis 值。
  2. 执行 ? RequireInternalSlot(O, [[RegExpMatcher]])。
  3. pattern 是一个对象且具有 [[RegExpMatcher]] 内部槽,则
    1. flags 不为 undefined,抛出 TypeError 异常。
    2. Ppattern.[[OriginalSource]]
    3. Fpattern.[[OriginalFlags]]
  4. 否则,
    1. Ppattern
    2. Fflags
  5. 返回 ? RegExpInitialize(O, P, F)。
Note

该方法使用新的模式与标志完全重新初始化 this RegExp。实现可以将使用此方法视为对结果 RegExp 对象将被多次使用的断言,从而可进行额外优化。

B.3 其他附加特性

B.3.1 带标签的函数声明

在 ECMAScript 2015 之前,LabelledStatement 的规范不允许将语句标签与 FunctionDeclaration 关联。然而,带标签的 FunctionDeclaration 是非严格代码的允许扩展,且多数浏览器宿主 ECMAScript 实现支持该扩展。ECMAScript 2015 及之后,LabelledStatement 的语法产生式允许将 FunctionDeclaration 用作 LabelledItem,但 14.13.1 包含的早期错误规则在发生时产生语法错误。然后该规则被修改,在宿主支持该特性的非严格代码中抑制该语法错误。

Note

WithStatementIfStatementIterationStatement早期错误规则阻止它们在非严格代码中包含带标签的 FunctionDeclaration

B.3.2 块级函数声明 Web 历史兼容语义

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

  1. 在单个块内声明并仅在该块内引用函数。

  2. 在单个 Block 中声明函数并可能使用,同时被不在该 Block 内的内部函数引用。

    • 一个或多个 FunctionDeclaration,其 BindingIdentifier 名称为 f,出现在封闭函数 g 的函数代码内,且该声明嵌套在一个 Block 中。
    • g 的函数代码内不存在其他非 varf 声明。
    • 在包含 f 声明的 BlockStatementList 内可以有 f 作为 IdentifierReference 的出现。
    • 在嵌套于 g 之内的另一个函数 h 中至少有一次 f 作为 IdentifierReference 的出现,且不存在其他 f 声明屏蔽 h 内对 f 的引用。
    • 所有对 h 的调用都发生在 f 声明已被求值之后。
  3. 在单个块中声明函数并可能使用,同时也在后续块中被引用。

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

第二与第三种用例的 ECMAScript 2015 互操作性需要对 101519.2.116.1.7 章节语义做出以下扩展。

如果 ECMAScript 实现有报告诊断警告消息的机制,当代码包含应用这些兼容语义并对非兼容语义引入可观察差异的 FunctionDeclaration 时应产生警告。例如,如果由于引入会造成早期错误而未引入 var 绑定,则不应产生警告。

该特性在以下点涉及特殊语义:

B.3.3 IfStatement 语句子句中的 FunctionDeclarations

以下内容扩展了 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 中规定的网页历史兼容语义。

B.3.4 Catch 块中的 VariableStatements

该特性中,Catch 子句的 Block 可以包含将名称绑定到也由 CatchParameter 绑定的名称的 var 声明。通过修改 Catch : catch ( CatchParameter ) Block 14.15.1 中的一条早期错误规则实现。

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.1 中 ContainsDuplicateLabels 的静态语义扩展如下:

ForInOfStatement : for ( var BindingIdentifier Initializer in Expression ) Statement
  1. 返回 Statement 的 ContainsDuplicateLabels,参数 labelSet

8.3.2 中 ContainsUndefinedBreakTarget 的静态语义扩展如下:

ForInOfStatement : for ( var BindingIdentifier Initializer in Expression ) Statement
  1. 返回 Statement 的 ContainsUndefinedBreakTarget,参数 labelSet

8.3.3 中 ContainsUndefinedContinueTarget 的静态语义扩展如下:

ForInOfStatement : for ( var BindingIdentifier Initializer in Expression ) Statement
  1. 返回 Statement 的 ContainsUndefinedContinueTarget,参数 iterationSet 与 « »。

14.7.5.2 中 IsDestructuring 的静态语义扩展如下:

BindingIdentifier : Identifier yield await
  1. 返回 false

8.2.6 中 VarDeclaredNames 的静态语义扩展如下:

ForInOfStatement : for ( var BindingIdentifier Initializer in Expression ) Statement
  1. names1BindingIdentifier 的 BoundNames。
  2. names2Statement 的 VarDeclaredNames。
  3. 返回 names1names2 的列表串联。

8.2.7 中 VarScopedDeclarations 的静态语义扩展如下:

ForInOfStatement : for ( var BindingIdentifier Initializer in Expression ) Statement
  1. declarations1 为 « BindingIdentifier »。
  2. declarations2Statement 的 VarScopedDeclarations。
  3. 返回 declarations1declarations2 的列表串联。

14.7.5.5 中 ForInOfLoopEvaluation 的运行时语义扩展如下:

ForInOfStatement : for ( var BindingIdentifier Initializer in Expression ) Statement
  1. bindingIdBindingIdentifier 的 StringValue。
  2. lhs 为 ? ResolveBinding(bindingId)。
  3. 若 IsAnonymousFunctionDefinition(Initializer) 为 true,则
    1. value 为 ? NamedEvaluation(Initializer, 参数 bindingId)。
  4. 否则,
    1. rhs 为 ? Evaluation(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]] internal slot。具有 [[IsHTMLDDA]] 内部槽的对象在 ToBooleanIsLooselyEqual 抽象操作中以及作为 typeof 运算符 操作数时表现得像 undefined

Note

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

该特性在以下点涉及特殊语义:

B.3.7 HostMakeJobCallback 中的非默认行为

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

B.3.8 HostEnsureCanAddPrivateElement 中的非默认行为

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

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

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

Note

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

参见 AssignmentTargetType 的步骤 1,适用于 CallExpression : CoverCallExpressionAndAsyncArrowHead CallExpression : CallExpression Arguments