These operations are not a part of the ECMAScript language; they are defined here to solely to aid the specification of the semantics of the ECMAScript language. Other, more specialized abstract operations are defined throughout this specification.
+这些操作不是 ECMAScript 语言的一部分;在这里定义他们只是为了规范 ECMAScript 语言的语义。另外,本规范中还定义了其他特定的抽象操作。
The ECMAScript language implicitly performs automatic type conversion as needed. To clarify the semantics of certain constructs it is useful to define a set of conversion abstract operations. The conversion abstract operations are polymorphic; they can accept a value of any ECMAScript language type. But no other specification types are used with these operations.
+ECMAScript 语言根据需要会自动执行类型转换。为了解释清楚某些结构的语义,定义一组转换抽象操作是很有用的。转换抽象操作是多态的;他们可以接受一个任意 ECMAScript 类型的值。但是这些操作中没有使用其他相应的规范类型。
The abstract operation ToPrimitive takes an _input_ argument and an optional argument _PreferredType_. The abstract operation ToPrimitive converts its _input_ argument to a non-Object type. If an object is capable of converting to more than one primitive type, it may use the optional hint _PreferredType_ to favour that type. Conversion occurs according to the following algorithm:
+抽象操作 ToPrimitive 接受一个 _input_ 参数和一个可选的 _PreferredType_ 参数。ToPrimitive 负责把 _input_ 参数转换为一个非对象类型。如果一个对象可以转换为多个原始类型,则应该根据可选提示 _PreferredType_ 来判断转换的类型。根据以下算法进行转换:
When ToPrimitive is called with no hint, then it generally behaves as if the hint were Number. However, objects may over-ride this behaviour by defining a @@toPrimitive method. Of the objects defined in this specification only Date objects (see
当调用 ToPrimitive 没有提示参数时,通常表现和提示为 Number 类型一样。然而,对象可能通过定义一个 @@toPrimitive 方法,来覆盖这一行为。在本规范中的对象只有 Date 对象 (见
When the abstract operation OrdinaryToPrimitive is called with arguments _O_ and _hint_, the following steps are taken:
+当使用参数 _O_ 和 _hint_ 调用抽象操作 OrdinaryToPrimitive 时,执行以下步骤:
The abstract operation ToBoolean converts _argument_ to a value of type Boolean according to
抽象操作 ToBoolean 把参数 _argument_ 转换为一个 Boolean 类型的值,见表
- Argument Type + 参数类型 | - Result + 结果 |
---|---|
- Return *false*. + 返回 *false*。 | |
- Return *false*. + 返回 *false*。 | |
- Return _argument_. + 返回 _argument_。 | |
- If _argument_ is *+0*, *-0*, or *NaN*, return *false*; otherwise return *true*. + 如果 _argument_ 为 *+0*,*-0*,or *NaN*,返回 *false*; 否则返回 *true*。 | |
- If _argument_ is the empty String (its length is zero), return *false*; otherwise return *true*. + 如果 _argument_ 是一个空字符串 (长度为0的字符串), 返回 *false*; 否则返回 *true*。 | |
- Return *true*. + 返回 *true*。 | |
- Return *true*. + 返回 *true*。 |
- Argument Type + 参数类型 | - Result + 结果 |
---|---|
- Return *NaN*. + 返回 *NaN*。 | |
- Return *+0*. + 返回 *+0*。 | |
- If _argument_ is *true*, return 1. If _argument_ is *false*, return *+0*. + 如果 _argument_ 是 *true*,返回 1。如果 _argument_ 是 *false*,返回 *+0*。 | |
- Return _argument_ (no conversion). + 返回 _argument_ (不转换)。 | |
- See grammar and conversion algorithm below. + 见下面语法和转换算法。 | |
- Throw a *TypeError* exception. + 抛出一个 *TypeError* 异常。 | |
- Apply the following steps: +应用以下步骤: ToNumber ( _argument_ )ToNumber Applied to the String Type-ToNumber applied to Strings applies the following grammar to the input String interpreted as a sequence of UTF-16 encoded code points ( 应用于 String 类型的 ToNumber+应用 ToNumber 的字符串使用如下语法把字符串解释为一个 UTF-16 的编码码点 ( The terminal symbols of this grammar are all composed of Unicode BMP code points so the result will be *NaN* if the string contains the UTF-16 encoding of any supplementary code points or any unpaired surrogate code points. +这个语法的终结符都由 Unicode BMP 码点组成,所以如果字符串包含任何补充码点或任何未配对替代码点的 UTF-16 编码,那么结果就会是 NaN。 SyntaxSyntax`.` DecimalDigits ExponentPart? DecimalDigits ExponentPart?All grammar symbols not explicitly defined above have the definitions used in the Lexical Grammar for numeric literals ( 以上所有未明确定义的语法符号都在数字字面量词法定义( Some differences should be noted between the syntax of a |StringNumericLiteral| and a |NumericLiteral|: +应该注意 |StringNumericLiteral| 和 |NumericLiteral| 语法之间的一些区别:
Runtime Semantics: MV-The conversion of a String to a Number value is similar overall to the determination of the Number value for a numeric literal (see 运行时语义: MV+字符串到 Number 值的转换和一个的数字字面量 Number 值的确定总体上来说是相似的(见
Once the exact MV for a String numeric literal has been determined, it is then rounded to a value of the Number type. If the MV is 0, then the rounded value is *+0* unless the first non white space code point in the String numeric literal is `"-"`, in which case the rounded value is *-0*. Otherwise, the rounded value must be the Number value for the MV (in the sense defined in 一旦确定了字符串数字字面量确切的数值,它将被舍入为一个 Number 类型的值。如果数值为 0,那么它舍入的值为 *+0*,除非字符串数字字面量的第一个非空格码点是 `"-"`,这种情况舍入值为 *-0*。否则,舍入的值必须为一个数字类型的数值(定义在
Runtime Semantics: MVToInteger ( _argument_ )-The abstract operation ToInteger converts _argument_ to an integral numeric value. This abstract operation functions as follows: +抽象操作 ToInteger 把 _argument_ 转换为整数数值。这个抽象操作算法如下: ToInteger ( _argument_ )ToInt32 ( _argument_ )-The abstract operation ToInt32 converts _argument_ to one of 232 integer values in the range 抽象操作 ToInt32 把 _argument_ 转换为范围为 ToInt32 ( _argument_ )1. If _int32bit_ ≥ 231, return _int32bit_ - 232; otherwise return _int32bit_.Given the above definition of ToInt32: +鉴于以上 ToInt32 的定义:
|