我有以下 ECMAScript 6 模板文字的代码:
let person = {name: 'John Smith'};
let tpl = `My name is ${person.name}.`;
let MyVar = "My name is " + person.name + ".";
console.log("template literal= " + tpl);
console.log("my variable = " + MyVar);
产出如下:
template literal= My name is John Smith.
my variable = My name is John Smith.
我试图找到确切的区别,但是找不到, 以下两种说法的区别是什么?
let tpl = `My name is ${person.name}.`;
And
let MyVar = "My name is "+ person.name+".";
我已经能够在这里获得与 person.name
连接的字符串 MyVar
,那么在什么场景中使用模板文字呢?