Multipage preference

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 Static Semantics: 早期错误

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

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

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

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

B.1.2.2 Static Semantics: CountLeftCapturingParensWithin 和 CountLeftCapturingParensBefore

在 CountLeftCapturingParensWithin 和 CountLeftCapturingParensBefore 的定义中,对“ 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. 令 i 为 codePoint 的数值。
  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. 令 char 为 ExtendedPatternCharacter 表示的字符。
  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 为实参的第一个 ClassAtom 的 CompileToCharSet。
  2. 令 otherSet 为以 regexpRecord 为实参的第二个 ClassAtom 的 CompileToCharSet。
  3. 令 remainingSet 为以 regexpRecord 为实参的 ClassContents 的 CompileToCharSet。
  4. 令 rangeSet 为 CharacterRangeOrUnion(regexpRecord, charSet, otherSet)。
  5. 返回 rangeSet 和 remainingSet 的并集。
NonemptyClassRangesNoDash :: ClassAtomNoDash - ClassAtom ClassContents
  1. 令 charSet 为以 regexpRecord 为实参的 ClassAtomNoDash 的 CompileToCharSet。
  2. 令 otherSet 为以 regexpRecord 为实参的 ClassAtom 的 CompileToCharSet。
  3. 令 remainingSet 为以 regexpRecord 为实参的 ClassContents 的 CompileToCharSet。
  4. 令 rangeSet 为 CharacterRangeOrUnion(regexpRecord, charSet, otherSet)。
  5. 返回 rangeSet 和 remainingSet 的并集。

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

ClassEscape :: c ClassControlLetter
  1. 令 charValue 为此 ClassEscape 的 CharacterValue。
  2. 令 char 为字符值为 charValue 的字符。
  3. 返回包含单个字符 char 的 CharSet。
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 charSet、otherSet 和 remainingSet 的并集。
  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. 如果 v 是 true 且 u 是 true,则
    1. 令 parseResult 为包含一个或多个 SyntaxError 对象的 List。
  2. 否则,如果 v 是 true,则
    1. 令 parseResult 为 ParseText(patternText, Pattern[+UnicodeMode, +UnicodeSetsMode, +NamedCaptureGroups])。
  3. 否则,如果 u 是 true,则
    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 宿主是 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. 令 length 为 string 的长度。
  3. 令 result 为空 String。
  4. 令 unescapedSet 为 ASCII word characters 和 "@*+-./" 的字符串连接。
  5. 令 k 为 0。
  6. 重复,只要 k < length,
    1. 令 codeUnit 为 string 中索引 k 处的代码单元。
    2. 如果 unescapedSet 包含 codeUnit,则
      1. 令 nextPart 为 codeUnit。
    3. 否则,
      1. 令 codeUnitNumber 为 codeUnit 的数值。
      2. 如果 codeUnitNumber < 256,则
        1. 令 hex 为 codeUnitNumber 的 String 表示,格式化为大写十六进制数。
        2. 令 nextPart 为 "%" 和 StringPad(hex, 2, "0", start) 的字符串连接。
      3. 否则,
        1. 令 hex 为 codeUnitNumber 的 String 表示,格式化为大写十六进制数。
        2. 令 nextPart 为 "%u" 和 StringPad(hex, 4, "0", start) 的字符串连接。
    4. 将 result 设置为 result 和 nextPart 的字符串连接。
    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. 令 length 为 string 的长度。
  3. 令 result 为空 String。
  4. 令 k 为 0。
  5. 重复,只要 k < length,
    1. 令 codeUnit 为 string 中索引 k 处的代码单元。
    2. 如果 codeUnit 是代码单元 0x0025(PERCENT SIGN),则
      1. 令 hexDigits 为空 String。
      2. 令 optionalAdvance 为 0。
      3. 如果 k + 5 < length 且 string 中索引 k + 1 处的代码单元是代码单元 0x0075(LATIN SMALL LETTER U),则
        1. 将 hexDigits 设置为 string 从 k + 2 到 k + 6 的子串。
        2. 将 optionalAdvance 设置为 5。
      4. 否则,如果 k + 3 ≤ length,则
        1. 将 hexDigits 设置为 string 从 k + 1 到 k + 3 的子串。
        2. 将 optionalAdvance 设置为 2。
      5. 令 parseResult 为 ParseText(hexDigits, HexDigits[~Sep])。
      6. 如果 parseResult 是 Parse Node,则
        1. 令 codeUnitNumber 为 parseResult 的 MV。
        2. 将 codeUnit 设置为其数值为 codeUnitNumber 的代码单元。
        3. 将 k 设置为 k + optionalAdvance。
    3. 将 result 设置为 result 和 codeUnit 的字符串连接。
    4. 将 k 设置为 k + 1。
  6. 返回 result。

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

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

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

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

  1. 令 obj 为 this 值。
  2. 执行 ? RequireObjectCoercible(obj)。
  3. 令 string 为 ? ToString(obj)。
  4. 令 size 为 string 的长度。
  5. 令 intStart 为 ? ToClampedIndex(start, size)。
  6. 若 length 为 undefined,令 intLength 为 size;否则令 intLength 为将 ? ToIntegerOrInfinity(length) 在 0 和 size 之间钳制后的结果。
  7. 令 intEnd 为 min(intStart + intLength, size)。
  8. 返回 string 从 intStart 到 intEnd 的子字符串。
Note

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

B.2.2.2 String.prototype.anchor ( name )

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

  1. 令 string 为 this 值。
  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. 令 part2 为 part1 和 ">" 的字符串连接。
  6. 令 part3 为 part2 和 contentsString 的字符串连接。
  7. 令 part4 为 part3、"</"、tag 和 ">" 的字符串连接。
  8. 返回 part4。

B.2.2.3 String.prototype.big ( )

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

  1. 令 string 为 this 值。
  2. 返回 ? CreateHTML(string, "big", "", "")。

B.2.2.4 String.prototype.blink ( )

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

  1. 令 string 为 this 值。
  2. 返回 ? CreateHTML(string, "blink", "", "")。

B.2.2.5 String.prototype.bold ( )

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

  1. 令 string 为 this 值。
  2. 返回 ? CreateHTML(string, "b", "", "")。

B.2.2.6 String.prototype.fixed ( )

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

  1. 令 string 为 this 值。
  2. 返回 ? CreateHTML(string, "tt", "", "")。

B.2.2.7 String.prototype.fontcolor ( colour )

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

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

B.2.2.8 String.prototype.fontsize ( size )

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

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

B.2.2.9 String.prototype.italics ( )

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

  1. 令 string 为 this 值。
  2. 返回 ? CreateHTML(string, "i", "", "")。

B.2.2.10 String.prototype.link ( url )

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

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

B.2.2.11 String.prototype.small ( )

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

  1. 令 string 为 this 值。
  2. 返回 ? CreateHTML(string, "small", "", "")。

B.2.2.12 String.prototype.strike ( )

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

  1. 令 string 为 this 值。
  2. 返回 ? CreateHTML(string, "strike", "", "")。

B.2.2.13 String.prototype.sub ( )

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

  1. 令 string 为 this 值。
  2. 返回 ? CreateHTML(string, "sub", "", "")。

B.2.2.14 String.prototype.sup ( )

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

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

B.2.3.2 Date.prototype.setYear ( year )

Note

对于几乎所有用途,都应优先使用 setFullYear 方法,因为它可以避免“2000 年问题”。

调用此方法时,它执行以下步骤:

  1. 令 dateObj 为 this 值。
  2. 执行 ? RequireInternalSlot(dateObj, [[DateValue]])。
  3. 令 time 为 dateObj.[[DateValue]]。
  4. 令 year 为 ? ToNumber(year)。
  5. 如果 time 为 NaN,则将 time 设置为 +0𝔽;否则将 time 设置为 LocalTime(time)。
  6. 令 fullYear 为 MakeFullYear(year)。
  7. 令 day 为 MakeDay(fullYear, 𝔽(MonthFromTime(time)), 𝔽(DateFromTime(time)))。
  8. 令 date 为 MakeDate(day, 𝔽(TimeWithinDay(time)))。
  9. 令 utcTimestamp 为 TimeClip(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. 令 obj 为 this 值。
  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

WithStatement、IfStatement 和 IterationStatement 的早期错误规则阻止这些语句在非严格代码中包含带标签的 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 声明。
    • 作为 IdentifierReference 的 f 可能出现在包含 f 声明的 Block 的 StatementList 内。
    • 在嵌套于 g 内的另一个函数 h 中,至少有一次作为 IdentifierReference 的 f 出现,并且没有其他 f 声明遮蔽来自 h 内部的对 f 的引用。
    • h 的所有调用都发生在 f 的声明已被求值之后。
  3. 函数在单个块内声明并可能使用,但也在后续块中被引用。

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

第二和第三个用例的 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 调用所引入的 var 和 function 声明。此变更通过修改 EvalDeclarationInstantiation 中的步骤 3.d.i.2.a.i 和 13.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. 返回以 labelSet 为实参的 Statement 的 ContainsDuplicateLabels。

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

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

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

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

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

BindingIdentifier : Identifier yield await
  1. 返回 false。

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

ForInOfStatement : for ( var BindingIdentifier Initializer in Expression ) Statement
  1. 令 names1 为 BindingIdentifier 的 BoundNames。
  2. 令 names2 为 Statement 的 VarDeclaredNames。
  3. 返回 names1 和 names2 的列表连接。

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

ForInOfStatement : for ( var BindingIdentifier Initializer in Expression ) Statement
  1. 令 decls1 为 « BindingIdentifier »。
  2. 令 decls2 为 Statement 的 VarScopedDeclarations。
  3. 返回 decls1 和 decls2 的列表连接。

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

ForInOfStatement : for ( var BindingIdentifier Initializer in Expression ) Statement
  1. 令 bindingId 为 BindingIdentifier 的 StringValue。
  2. 令 lhs 为 ? ResolveBinding(bindingId)。
  3. 如果 IsAnonymousFunctionDefinition(Initializer) 是 true,则
    1. 令 value 为以 bindingId 为实参的 Initializer 的 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]] 内部槽的对象,在 ToBoolean 和 IsLooselyEqual 抽象操作中,以及作为 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

当赋值目标是 AssignmentExpression 的 LeftHandSideExpression 时,赋值运算符必须是 = 或 AssignmentOperator;特别地,这里的允许不适用于逻辑赋值运算符(??=、&&=、||=)。

见 AssignmentTargetType 中 CallExpression : CoverCallExpressionAndAsyncArrowHead 和 CallExpression : CallExpression Arguments 的步骤 1。