猿问

请问下 ecma 里 ToPrimitive([]) 怎么把hint 转成 string 的

为了解决这个问题: [] + [] === "", 
请问下 ToPrimitive([]) 怎么把hint 转成 string 的

ToPrimitive ( input [ , PreferredType ] )

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:

  1. Assert: input is an ECMAScript language value.

  2. If Type(input) is Object, then

    1. Let result be ? Call(exoticToPrim, input, « hint »).

    2. If Type(result) is not Object, return result.

    3. Throw a TypeError exception.

    1. If PreferredType is not present, let hint be "default".

    2. Else if PreferredType is hint String, let hint be "string".

    3. Else PreferredType is hint Number, let hint be "number".

    4. Let exoticToPrim be ? GetMethod(input, @@toPrimitive).

    5. If exoticToPrim is not undefined, then

    6. If hint is "default", set hint to "number".

    7. Return ? OrdinaryToPrimitive(input, hint).

  3. Return input.


沧海一幻觉
浏览 426回答 1
1回答
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答