页面模式偏好

附录 B (规范性) Web 浏览器的附加 ECMAScript 特性

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

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

当某个特性在主体中规定时,其影响本文档的每个位置都会在有色框中标有“Normative Optional”字样。此外,如果该特性涉及算法或提前错误规则中的特定措辞,则会以“宿主支持”相关特性这一条件进行保护。Web 浏览器必须支持所有此类特性。

注

本附录描述 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 _ 注

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

B.1.2.1 Static Semantics: 提前错误

22.2.1.1 的语义扩展如下:

ExtendedAtom :: InvalidBracedQuantifier
  • 如果有任何源文本被此产生式匹配,则为 Syntax Error。

此外,以下产生式的规则通过添加突出显示的文本进行修改:

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

IsCharacterClass 的语义扩展如下:

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

B.1.2.4 Static Semantics: CharacterValue

CharacterValue 的语义扩展如下:

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

B.1.2.5 Runtime Semantics: CompileSubpattern

带参数 regexpRecord 和 direction 的 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

带参数 regexpRecord 的 CompileAssertion 语义扩展如下:

QuantifiableAssertion :: (?= Disjunction ) 的规则与 Assertion :: (?= Disjunction ) 相同,但用 QuantifiableAssertion 替换 Atom。

QuantifiableAssertion :: (?! Disjunction ) 的规则与 Assertion :: (?! Disjunction ) 相同,但用 QuantifiableAssertion 替换 Atom。

B.1.2.7 Runtime Semantics: CompileAtom

带参数 regexpRecord 和 direction 的 CompileAtom 语义扩展如下:

除 Atom :: PatternCharacter 外,Atom 产生式的规则也用于 ExtendedAtom 产生式,但用 ExtendedAtom 替换 Atom。

还会添加以下规则:

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

带参数 regexpRecord 的 CompileToCharSet 语义扩展如下:

替换以下两个规则:

NonemptyClassRanges :: ClassAtom - ClassAtom ClassContents
  1. 令 charSet 为 CompileToCharSet of the first ClassAtom with argument regexpRecord。
  2. 令 otherSet 为 CompileToCharSet of the second ClassAtom with argument regexpRecord。
  3. 令 remainingSet 为 CompileToCharSet of ClassContents with argument regexpRecord。
  4. 令 rangeSet 为 CharacterRangeOrUnion(regexpRecord, charSet, otherSet)。
  5. 返回 rangeSet 和 remainingSet 的并集。
NonemptyClassRangesNoDash :: ClassAtomNoDash - ClassAtom ClassContents
  1. 令 charSet 为 CompileToCharSet of ClassAtomNoDash with argument regexpRecord。
  2. 令 otherSet 为 CompileToCharSet of ClassAtom with argument regexpRecord。
  3. 令 remainingSet 为 CompileToCharSet of ClassContents with argument regexpRecord。
  4. 令 rangeSet 为 CharacterRangeOrUnion(regexpRecord, charSet, otherSet)。
  5. 返回 rangeSet 和 remainingSet 的并集。

还会添加以下规则:

ClassEscape :: c ClassControlLetter
  1. 令 charValue 为 the CharacterValue of this ClassEscape。
  2. 令 char 为字符值为 charValue 的字符。
  3. 返回包含单个字符 char 的 CharSet。
ClassAtomNoDash :: \ [lookahead = c]
  1. 返回包含单个字符 \ U+005C (REVERSE SOLIDUS) 的 CharSet。
注
此产生式只能从字符类中的序列 \c 到达,并且该序列后面没有可接受的控制字符。

B.1.2.8.1 CharacterRangeOrUnion ( regexpRecord, charSet, otherSet )

抽象操作 CharacterRangeOrUnion,接收参数 regexpRecord (RegExp 记录)、charSet (CharSet) 和 otherSet (CharSet);返回值:CharSet。 调用时执行以下步骤:

  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 )

带参数 patternText、u 和 v 的 ParsePattern 语义扩展如下,其中修改内容以突出显示:

  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 contains a GroupName,则
      1. 将 parseResult 设置为 ParseText(patternText, Pattern[~UnicodeMode, ~UnicodeSetsMode, +NamedCaptureGroups])。
  5. 返回 parseResult。

B.2 附加内置属性

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

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

将 表 103 中的条目添加到 表 6。

表 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。
注

此编码部分基于 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 为 the MV of parseResult。
        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 value 转换为 String 的结果的一个子字符串,从索引 start 开始并持续 length 个码元(如果 length 是 undefined,则持续到 String 末尾)。如果 start 为负数,则将其视为 sourceLength + start,其中 sourceLength 是 String 的长度。结果是 String 值,而不是 String 对象。

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

  1. 令 obj 为 this value。
  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 的子字符串。
注

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

B.2.2.2 String.prototype.anchor ( name )

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

  1. 令 string 为 this value。
  2. 返回 ? CreateHTML(string, "a", "name", name)。

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

抽象操作 CreateHTML,接收参数 contents (ECMAScript 语言值)、tag (String)、attr (String) 和 attrValue (ECMAScript 语言值);返回值:(正常完成 (值:String) 或 抛出完成)。 调用时执行以下步骤:

  1. 执行 ? RequireObjectCoercible(contents)。
  2. 令 contentsString 为 ? ToString(contents)。
  3. 令 part1 为 "<" 和 tag 的字符串连接。
  4. 如果 attr 不是空 String,则
    1. 令 attrValueString 为 ? ToString(attrValue)。
    2. 令 escapedAttrValue 为一个 String 值,它与 attrValueString 相同,但 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 value。
  2. 返回 ? CreateHTML(string, "big", "", "")。

B.2.2.4 String.prototype.blink ( )

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

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

B.2.2.5 String.prototype.bold ( )

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

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

B.2.2.6 String.prototype.fixed ( )

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

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

B.2.2.7 String.prototype.fontcolor ( colour )

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

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

B.2.2.8 String.prototype.fontsize ( size )

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

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

B.2.2.9 String.prototype.italics ( )

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

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

B.2.2.10 String.prototype.link ( url )

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

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

B.2.2.11 String.prototype.small ( )

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

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

B.2.2.12 String.prototype.strike ( )

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

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

B.2.2.13 String.prototype.sub ( )

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

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

B.2.2.14 String.prototype.sup ( )

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

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

B.2.2.15 String.prototype.trimLeft ( )

注

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

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

B.2.2.16 String.prototype.trimRight ( )

注

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

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

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

B.2.3.1 Date.prototype.getYear ( )

注

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

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

  1. 令 dateObj 为 this value。
  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 )

注

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

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

  1. 令 dateObj 为 this value。
  2. 执行 ? RequireInternalSlot(dateObj, [[DateValue]])。
  3. 令 time 为 dateObj.[[DateValue]]。
  4. 如果 time 是 NaN,则将 time 设置为 +0𝔽;否则将 time 设置为 LocalTime(time)。
  5. 令 fullYear 为 MakeFullYear(? ToNumber(year))。
  6. 令 day 为 MakeDay(fullYear, 𝔽(MonthFromTime(time)), 𝔽(DateFromTime(time)))。
  7. 令 date 为 MakeDate(day, 𝔽(TimeWithinDay(time)))。
  8. 令 utcTimestamp 为 TimeClip(UTC(date))。
  9. 将 dateObj.[[DateValue]] 设置为 utcTimestamp。
  10. 返回 utcTimestamp。

B.2.3.3 Date.prototype.toGMTString ( )

注

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

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

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

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

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

  1. 令 obj 为 this value。
  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)。
注

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

B.3 其他附加特性

B.3.1 带标签的函数声明

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

注

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

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

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

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

  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 的一条提前错误规则来实现。

注

在运行时,此类绑定会在 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]

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

带参数 labelSet 的 ContainsDuplicateLabels 静态语义扩充如下:

ForInOfStatement : for ( var BindingIdentifier Initializer in Expression ) Statement
  1. 返回 ContainsDuplicateLabels of Statement with argument labelSet。

带参数 labelSet 的 ContainsUndefinedBreakTarget 静态语义扩充如下:

ForInOfStatement : for ( var BindingIdentifier Initializer in Expression ) Statement
  1. 返回 ContainsUndefinedBreakTarget of Statement with argument labelSet。

带参数 iterationSet 和 labelSet 的 ContainsUndefinedContinueTarget 静态语义扩充如下:

ForInOfStatement : for ( var BindingIdentifier Initializer in Expression ) Statement
  1. 返回 ContainsUndefinedContinueTarget of Statement with arguments iterationSet and « »。

IsDestructuring 静态语义扩充如下:

BindingIdentifier : Identifier yield await
  1. 返回 false。

VarDeclaredNames 静态语义扩充如下:

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

VarScopedDeclarations 静态语义扩充如下:

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

带参数 labelSet 的 ForInOfLoopEvaluation 运行时语义扩充如下:

ForInOfStatement : for ( var BindingIdentifier Initializer in Expression ) Statement
  1. 令 name 为 the StringValue of BindingIdentifier。
  2. 令 lhs 为 ? ResolveBinding(name)。
  3. 如果 IsAnonymousFunctionDefinition(Initializer) 是 true,则
    1. 令 value 为 ? NamedEvaluation of Initializer with argument name。
  4. 否则,
    1. 令 rhs 为 ? Evaluation of 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。

注

本规范从不创建具有 [[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 异常。

注

当赋值目标是 AssignmentExpression 的 LeftHandSideExpression 时,赋值运算符必须是 = 或 AssignmentOperator;尤其是,此处的允许不适用于逻辑赋值运算符(??=、&&=、||=)。

参见 AssignmentTargetType 针对 CallExpression : CoverCallExpressionAndAsyncArrowHead 和 CallExpression : CallExpression Arguments 的步骤 1。