是否保证JavaScript模板文字可以调用toString()? [英] Are JavaScript template literals guaranteed to call toString()?

查看:56
本文介绍了是否保证JavaScript模板文字可以调用toString()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

const num = 42
const str = `My number is ${num}`

在此代码中,我有什么保证将 num 转换为 string ?

In this code what guarantee do I have about the conversion of num to a string ?

是否保证只调用其 toString()方法,还是可以通过其他方式进行转换?

Is it guaranteed to just call its toString() method or could the conversion be done in another way ?

推荐答案

未标记的模板使用ECMAScript ToString()抽象操作.模板文字评估的逻辑分布在几个部分中,因此很难遵循,因此我将发布一个链接:

Untagged templates use the ECMAScript ToString() abstract operation. The logic of template literal evaluation is spread over several sections which makes it difficult to follow, so I'll just post a link to it: https://tc39.es/ecma262/#sec-template-literals-runtime-semantics-evaluation

ToString(argument) 使用表代替算法步骤,因此我将在此处写出一些伪代码:

ToString(argument) uses a table instead of algorithmic steps, so I'll write out some pseudocode here:

switch (Type(argument)) {
  case 'Undefined':
    return 'undefined';
  case 'Null':
    return 'null';
  case 'Boolean':
    return argument ? 'true' : 'false';
  case 'Number':
    return Number::toString(argument);
  case 'String':
    return argument;
  case 'Symbol':
    throw new TypeError();
  case 'BigInt':
    return BigInt::toString(arugment);
  case 'Object':
    return ToString(ToPrimitive(argument, 'string'));
}

如您所见,原始值根本不执行js,引擎在内部创建字符串表示形式.对于对象,我们进入 ToPrimitive()算法.

As you can see, no js execution happens at all for primitive values, the engine internally creates a string representation. For objects, we go into the ToPrimitive() algorithm.

ToPrimitive(input,PreferredType) 会尝试从 input 中获取 Symbol.toPrimitive 方法,如果存在,则使用给定的 PreferredType 提示进行调用.如果 input 不具有 Symbol.toPrimitive 属性,则它会退回到 OrdinaryToPrimitive .

ToPrimitive(input, PreferredType) will try to get the Symbol.toPrimitive method from input, and if it's present, call it with the given PreferredType hint. If input does not have a Symbol.toPrimitive property, it falls back to OrdinaryToPrimitive.

OrdinrayToPrimitive(O,hint) 将尝试调用 toString valueOf 方法.如果 hint 'string',它将尝试首先调用 toString 方法,否则它将尝试调用 valueOf 方法优先.如果存在这些方法中的任何一个并且它们不返回对象,则将使用它们的返回值.如果都不存在或它们都返回对象,则将引发TypeError.

OrdinrayToPrimitive(O, hint) will try to call the toString and valueOf methods. If hint is 'string', it try to call toString method first, otherwise it will try to call the valueOf method first. If either of those methods are present and they don't return an object, their return value will be used. If neither are present or they both return objects, a TypeError will be thrown.

因此,要回答您的原始问题,转换 42 不会调用任何其他方法.引擎将在内部创建一个字符串表示形式( '42'),并使用它.

So to answer your original question, converting 42 will not call any other methods. The engine will internally create a string representation ('42'), and use that.

这篇关于是否保证JavaScript模板文字可以调用toString()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆