Dar*_*Joy 135 javascript string variables concatenation
我在这里或在MDN上没有看到任何东西.我确定我只是遗漏了一些东西.这个地方必须有一些文件吗?
从功能上看,它看起来允许您将变量嵌套在字符串中,而无需使用+运算符进行连接.我正在寻找有关此功能的文档.
例:
var string = 'this is a string';
console.log(`Insert a string here: ${string}`);
Run Code Online (Sandbox Code Playgroud)
Ric*_*yon 164
你在谈论模板文字.
它们允许多行字符串和字符串插值.
多行字符串:
console.log(`foo
bar`);
// foo
// barRun Code Online (Sandbox Code Playgroud)
字符串插值:
var foo = 'bar';
console.log(`Let's meet at the ${foo}`);
// Let's meet at the barRun Code Online (Sandbox Code Playgroud)
如上面的评论中所述,您可以在模板字符串/文字中包含表达式。例:
const one = 1;
const two = 2;
const result = `One add two is ${one + two}`;
console.log(result); // output: One add two is 3Run Code Online (Sandbox Code Playgroud)
您还可以使用模板文字执行隐式类型转换。例子:
let fruits = ["mango","orange","pineapple","papaya"];
console.log(`My favourite fruits are ${fruits}`);
// My favourite fruits are mango,orange,pineapple,papaya
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
98578 次 |
| 最近记录: |