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

类似于包含行终止符代码点的 MultiLineCommentSingleLineHTMLCloseComment 为了句法语法解析的目的被视为 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 Static Semantics: 早期错误

22.2.1.1 的语义按如下方式扩展:

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

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

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

B.1.2.2 Static Semantics: CountLeftCapturingParensWithin 和 CountLeftCapturingParensBefore

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

B.1.2.3 Static Semantics: IsCharacterClass

22.2.1.6 的语义按如下方式扩展:

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

B.1.2.4 Static Semantics: CharacterValue

22.2.1.7 的语义按如下方式扩展:

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

B.1.2.5 Runtime Semantics: 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 Runtime Semantics: CompileAssertion

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

B.1.2.7 Runtime Semantics: 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. charExtendedPatternCharacter 表示的字符。
  2. charSet包含字符 char 的单元素 CharSet
  3. 返回 CharacterSetMatcher(regexpRecord, charSet, false, direction)。

B.1.2.8 Runtime Semantics: CompileToCharSet

22.2.2.9 的语义按如下方式扩展:

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

NonemptyClassRanges :: ClassAtom - ClassAtom ClassContents
  1. charSet 为以 regexpRecord 为实参的第一个 ClassAtomCompileToCharSet
  2. otherSet 为以 regexpRecord 为实参的第二个 ClassAtomCompileToCharSet
  3. remainingSet 为以 regexpRecord 为实参的 ClassContentsCompileToCharSet
  4. rangeSetCharacterRangeOrUnion(regexpRecord, charSet, otherSet)。
  5. 返回 rangeSetremainingSet 的并集。
NonemptyClassRangesNoDash :: ClassAtomNoDash - ClassAtom ClassContents
  1. charSet 为以 regexpRecord 为实参的 ClassAtomNoDashCompileToCharSet
  2. otherSet 为以 regexpRecord 为实参的 ClassAtomCompileToCharSet
  3. remainingSet 为以 regexpRecord 为实参的 ClassContentsCompileToCharSet
  4. rangeSetCharacterRangeOrUnion(regexpRecord, charSet, otherSet)。
  5. 返回 rangeSetremainingSet 的并集。

此外,以下规则被添加到 CompileToCharSet

ClassEscape :: c ClassControlLetter
  1. charValue 为此 ClassEscapeCharacterValue
  2. char 为字符值为 charValue 的字符。
  3. 返回包含单个字符 charCharSet
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 (a RegExp Record), charSet (a CharSet), and otherSet (a CharSet) and returns a 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 Static Semantics: 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. 如果 parseResultParse NodeparseResult 包含 GroupName,则
      1. parseResult 设置为 ParseText(patternText, Pattern[~UnicodeMode, ~UnicodeSetsMode, +NamedCaptureGroups])。
  5. 返回 parseResult

B.2 附加内置属性

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

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

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

Table 103: 附加知名固有对象
固有对象名称 全局名称 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. lengthstring 的长度。
  3. result 为空 String。
  4. unescapedSet 为 ASCII word characters 和 "@*+-./"字符串连接
  5. k 为 0。
  6. 重复,只要 k < length
    1. codeUnitstring 中索引 k 处的代码单元。
    2. 如果 unescapedSet 包含 codeUnit,则
      1. nextPartcodeUnit
    3. 否则,
      1. codeUnitNumbercodeUnit 的数值。
      2. 如果 codeUnitNumber < 256,则
        1. hexcodeUnitNumber 的 String 表示,格式化为大写十六进制数。
        2. nextPart"%"StringPad(hex, 2, "0", start) 的字符串连接
      3. 否则,
        1. hexcodeUnitNumber 的 String 表示,格式化为大写十六进制数。
        2. nextPart"%u"StringPad(hex, 4, "0", start) 的字符串连接
    4. result 设置为 resultnextPart字符串连接
    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. lengthstring 的长度。
  3. result 为空 String。
  4. k 为 0。
  5. 重复,只要 k < length
    1. codeUnitstring 中索引 k 处的代码单元。
    2. 如果 codeUnit 是代码单元 0x0025(PERCENT SIGN),则
      1. hexDigits 为空 String。
      2. optionalAdvance 为 0。
      3. 如果 k + 5 < lengthstring 中索引 k + 1 处的代码单元是代码单元 0x0075(LATIN SMALL LETTER U),则
        1. hexDigits 设置为 stringk + 2 到 k + 6 的子串。
        2. optionalAdvance 设置为 5。
      4. 否则,如果 k + 3 ≤ length,则
        1. hexDigits 设置为 stringk + 1 到 k + 3 的子串。
        2. optionalAdvance 设置为 2。
      5. parseResultParseText(hexDigits, HexDigits[~Sep])。
      6. 如果 parseResultParse Node,则
        1. codeUnitNumberparseResult 的 MV。
        2. codeUnit 设置为其数值为 codeUnitNumber 的代码单元。
        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. string 为 ? ToString(obj)。
  4. sizestring 的长度。
  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. 返回 stringintStartintEnd 的子串。
Note

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

B.2.2.2 String.prototype.anchor ( name )

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

  1. stringthis 值。
  2. 返回 ? CreateHTML(string, "a", "name", name)。

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

The abstract operation CreateHTML takes arguments contents (an ECMAScript language value), tag (a String), attr (a String), and attrValue (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(contents)。
  2. contentsString 为 ? ToString(contents)。
  3. part1"<"tag字符串连接
  4. 如果 attr 不是空 String,则
    1. attrValueString 为 ? ToString(attrValue)。
    2. escapedAttrValue 为与 attrValueString 相同的 String 值,但 attrValueString 中每个代码单元 0x0022(QUOTATION MARK)的出现都被替换为六个代码单元序列 "&quot;"
    3. part1 设置为以下各项的字符串连接
      • part1
      • 代码单元 0x0020(SPACE)
      • attr
      • 代码单元 0x003D(EQUALS SIGN)
      • 代码单元 0x0022(QUOTATION MARK)
      • escapedAttrValue
      • 代码单元 0x0022(QUOTATION MARK)
  5. part2part1">"字符串连接
  6. part3part2contentsString字符串连接
  7. part4part3"</"tag">"字符串连接
  8. 返回 part4

B.2.2.3 String.prototype.big ( )

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

  1. stringthis 值。
  2. 返回 ? CreateHTML(string, "big", "", "")。

B.2.2.4 String.prototype.blink ( )

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

  1. stringthis 值。
  2. 返回 ? CreateHTML(string, "blink", "", "")。

B.2.2.5 String.prototype.bold ( )

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

  1. stringthis 值。
  2. 返回 ? CreateHTML(string, "b", "", "")。

B.2.2.6 String.prototype.fixed ( )

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

  1. stringthis 值。
  2. 返回 ? CreateHTML(string, "tt", "", "")。

B.2.2.7 String.prototype.fontcolor ( colour )

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

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

B.2.2.8 String.prototype.fontsize ( size )

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

  1. stringthis 值。
  2. 返回 ? CreateHTML(string, "font", "size", size)。

B.2.2.9 String.prototype.italics ( )

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

  1. stringthis 值。
  2. 返回 ? CreateHTML(string, "i", "", "")。

B.2.2.10 String.prototype.link ( url )

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

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

B.2.2.11 String.prototype.small ( )

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

  1. stringthis 值。
  2. 返回 ? CreateHTML(string, "small", "", "")。

B.2.2.12 String.prototype.strike ( )

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

  1. stringthis 值。
  2. 返回 ? CreateHTML(string, "strike", "", "")。

B.2.2.13 String.prototype.sub ( )

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

  1. stringthis 值。
  2. 返回 ? CreateHTML(string, "sub", "", "")。

B.2.2.14 String.prototype.sup ( )

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

  1. stringthis 值。
  2. 返回 ? CreateHTML(string, "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. dateObjthis 值。
  2. 执行 ? RequireInternalSlot(dateObj, [[DateValue]])。
  3. tvdateObj.[[DateValue]]
  4. 如果 tvNaN,则返回 NaN
  5. 返回 YearFromTime(LocalTime(tv)) - 1900𝔽

B.2.3.2 Date.prototype.setYear ( year )

Note

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

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

  1. dateObjthis 值。
  2. 执行 ? RequireInternalSlot(dateObj, [[DateValue]])。
  3. timedateObj.[[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. dateObj.[[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 包含一条早期错误规则,如果出现这种情况则产生语法错误。然后会修改该规则,以便在宿主支持此特性的情况下,在非严格代码中抑制该语法错误。

Note

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

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

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

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

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

    • 一个或多个 FunctionDeclaration 出现在外围函数 g 的函数代码中,其 BindingIdentifier 是名称 f,并且该声明嵌套在 Block 内。
    • g 的函数代码中没有其他不是 var 声明的 f 声明。
    • 作为 IdentifierReferencef 可能出现在包含 f 声明的 BlockStatementList 内。
    • 在嵌套于 g 内的另一个函数 h 中,至少有一次作为 IdentifierReferencef 出现,并且没有其他 f 声明遮蔽来自 h 内部的对 f 的引用。
    • h 的所有调用都发生在 f 的声明已被求值之后。
  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. 返回以 labelSet 为实参的 StatementContainsDuplicateLabels

8.3.2ContainsUndefinedBreakTarget静态语义通过以下内容扩充:

ForInOfStatement : for ( var BindingIdentifier Initializer in Expression ) Statement
  1. 返回以 labelSet 为实参的 StatementContainsUndefinedBreakTarget

8.3.3ContainsUndefinedContinueTarget静态语义通过以下内容扩充:

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

14.7.5.2IsDestructuring静态语义通过以下内容扩充:

BindingIdentifier : Identifier yield await
  1. 返回 false

8.2.8VarDeclaredNames静态语义通过以下内容扩充:

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

8.2.9VarScopedDeclarations静态语义通过以下内容扩充:

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

14.7.5.5ForInOfLoopEvaluation运行时语义通过以下内容扩充:

ForInOfStatement : for ( var BindingIdentifier Initializer in Expression ) Statement
  1. bindingIdBindingIdentifierStringValue
  2. lhs 为 ? ResolveBinding(bindingId)。
  3. 如果 IsAnonymousFunctionDefinition(Initializer) 是 true,则
    1. value 为以 bindingId 为实参的 InitializerNamedEvaluation
  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 对象是一个具有此槽的宿主定义 exotic object,它为 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