${}(美元符号和花括号)在 Javascript 的字符串中是什么意思?
问题描述:
我在这里或 MDN 上都没有看到任何东西.我确定我只是错过了一些东西.某处必须有一些关于此的文档?
I haven't seen anything here or on MDN. I'm sure I'm just missing something. There's got to be some documentation on this somewhere?
在功能上,它看起来允许您将变量嵌套在字符串中,而无需使用 +
运算符进行连接.我正在寻找有关此功能的文档.
Functionally, it looks like it allows you to nest a variable inside a string without doing concatenation using the +
operator. I'm looking for documentation on this feature.
示例:
var string = 'this is a string';
console.log(`Insert a string here: ${string}`);
答
你在谈论 模板文字.
它们允许多行字符串和字符串插值.
They allow for both multiline strings and string interpolation.
多行字符串:
console.log(`foo
bar`);
// foo
// bar
字符串插值:
var foo = 'bar';
console.log(`Let's meet at the ${foo}`);
// Let's meet at the bar