鍍金池/ 問答/網(wǎng)絡(luò)安全  HTML/ 請問下 ecma 里 ToPrimitive([]) 怎么把hint 轉(zhuǎn)成 st

請問下 ecma 里 ToPrimitive([]) 怎么把hint 轉(zhuǎn)成 string 的

為了解決這個問題: [] + [] === "",
請問下 ToPrimitive([]) 怎么把hint 轉(zhuǎn)成 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. 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

      1. Let?result?be ??Call(exoticToPrim,?input, ??hint??).
      2. If?Type(result) is not Object, return?result.
      3. Throw a?TypeError?exception.
    6. If?hint?is?"default", set?hint?to?"number".
    7. Return ??OrdinaryToPrimitive(input,?hint).
  3. Return?input.
回答
編輯回答
逗婦乳

自問自答

  1. ToPrimitive set hint to number
  2. OrdinaryToPrimitive 第4步let methodNames be ? "valueOf", "toString"
  3. 第5步[]先執(zhí)行 valueOf 返回[],not a primitive,繼續(xù)執(zhí)行 toString 返回""

參考這里評論,之前看到上面回答,沒注意到評論。。。

OrdinaryToPrimitive (?O,?hint?)

When the abstract operation OrdinaryToPrimitive is called with arguments?O?and?hint, the following steps are taken:

  1. Assert:?Type(O) is Object.
  2. Assert:?Type(hint) is String and its value is either?"string"?or?"number".
  3. If?hint?is?"string", then

    1. Let?methodNames?be ??"toString",?"valueOf"??.
  4. Else,

    1. Let?methodNames?be ??"valueOf",?"toString"??.
  5. For each?name?in?methodNames?in?List?order, do

    1. Let?method?be ??Get(O,?name).
    2. If?IsCallable(method) is?true, then

      1. Let?result?be ??Call(method,?O).
      2. If?Type(result) is not Object, return?result.
  6. Throw a?TypeError?exception.
2018年3月10日 17:03