Type Conversion
ToPrimitive
抽象操作 ToPrimitive
采用参数输入(ECMAScript语言值)和可选参数prferredType(STRING或NUMBER),
并返回包含ECMAScript语言值的normal completion或者throw completion。
它将其输入参数转换为非对象类型。
- 如果prferredType=STRING,则调用
toString
方法。 - 如果prferredType=NUMBER,则先调用
valueOf
方法,其次调用toString
方法。
执行步骤:
- If input is an Object, then
- Let exoticToPrim be ? GetMethod(input, @@toPrimitive).
- If exoticToPrim is not undefined, then
- If preferredType is not present, then
- Let hint be "default".
- Else if preferredType is STRING, then
- Let hint be "string".
- Else,
- Assert: preferredType is NUMBER.
- Let hint be "number".
- Let result be ? Call(exoticToPrim, input, « hint »).
- If result is not an Object, return result.
- Throw a TypeError exception.
- If preferredType is not present, let preferredType be NUMBER.
- Return ? OrdinaryToPrimitive(input, preferredType).
- Return input.
ToBoolean
抽象操作 ToBoolean
接收输入(ECMAScript语言值)的参数并且返回一个Boolean值。
执行步骤:
- If argument is a Boolean, return argument
- If argument is one of undefined, null, +0𝔽, -0𝔽, NaN, 0ℤ, or the empty String, return false.
- If argument is an Object and argument has an
[[IsHTMLDDA]]
internal slot, return false.
ToNumber
ToString
抽象操作符ToString
接收argument(ECMAScript language value)并且返回一个包含字符串的normal completion或者throw completion。
执行步骤:
- argument is String return argument
- argument is Symbol throw a TypeError exception
- argument is undefined, return 'undefined'
- argument is null, return 'null'
- argument is true, return 'true'
- argument is false, return 'false'
- argument is Number, return Number:toString
- argument is BigInt, return BigInt:toString
- Assert: argument is an Object
- Let PrimValue be ? ToPrimitive(argument, STRING)
- Assert: PrimValue is not an Object
- Return ? ToString(PrimValue)
实现如下,源码
export default function toString(it: any) {if(classof(it) === 'Symbol') throw new TypeError('Cannot convert a Symbol value to a string')return String(requireObjectCoercible(it))}
ToObject
抽象操作符ToObject
接收参数(ECMAScript language value)并且返回一个normal completion或者throw completion
转化表如下:
argument Type | Result |
---|---|
Undefined | Throw a TypeError exception |
Null | Throw a TypeError exception |
Boolean | return a new Boolean object whose [[BooleanData]] internal slot |
Number | return a new Number object whose [[NumberData]] internal slot |
String | return a new String object whose [[StringData]] internal slot |
Symbol | return a new Symbol object whose [[SymbolData]] internal slot |
BigInt | return a new BigInt object whose [[BigIntData]] internal slot |
Object | Return argument |
实现如下,源码实现
import requireObjectCoercible from "./require-object-coercible";// https://tc39.es/ecma262/#sec-toobjectexport default function toObject(it: any) {return Object(requireObjectCoercible(it))}
ToUint16
抽象操作符ToUint16
接收argument(ECMAScript language value)并且返回一个包含整数的normal completion或者throw completion
它将参数argument
转换为+0
到 216 - 1 包含区间中的216个整数值之一。
执行步骤:
- Let number be ? ToNumber(argument).
- If number is not finite or number is either or , return .
- Let int be truncate( ).
- Let int16bit be int modulo 216.
- Return 𝔽(int16bit).
代码实现
export const ToUint16 = (it: LanguageType) => {const number = Number(it); // actual ToNumberif(!isFinite(number) || isNaN(number) || number === 0) {return 0;}const int = Math.abs(number);const int16bit = int % 2**16;return Math.floor(int16bit)}
StringToBigInt
Conversion Symbols
- 𝔽(x): 数学值或者扩展的数学值
x
到Number值的转换。 - ℤ(x): 整数
x
到BigInt
的转换。 - ℝ(x): Number或者BigInt值
x
到数学值的转换。 - truncate(x): 通过向零舍入来移除x的小数部分,如果
x<0
,则产生-floor(-x)
,否则产生floor(x)
。