${}(美元符号和花括号)在 Javascript 的字符串中是什么意思? [英] What does ${} (dollar sign and curly braces) mean in a string in Javascript?

查看:80
本文介绍了${}(美元符号和花括号)在 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

这篇关于${}(美元符号和花括号)在 Javascript 的字符串中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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