在这里或MDN上我什么都没看到。我确定我只是想念一些东西。在某处必须有一些文档?
从功能上讲,它看起来像允许您将变量嵌套在字符串中,而无需使用+
运算符进行串联。我正在寻找有关此功能的文档。
例:
var string = 'this is a string';
console.log(`Insert a string here: ${string}`);
您正在谈论模板文字。
它们允许多行字符串和字符串插值。
多行字符串:
console.log(`foo
bar`);
// foo
// bar
字符串插值:
var foo = 'bar';
console.log(`Let's meet at the ${foo}`);
// Let's meet at the bar
如上面的评论中所述,您可以在模板字符串/文字中包含表达式。例:
const one = 1;
const two = 2;
const result = `One add two is ${one + two}`;
console.log(result); // output: One add two is 3
您还可以使用模板文字执行隐式类型转换。例:
let fruits = ["mango","orange","pineapple","papaya"];
console.log(`My favourite fruits are ${fruits}`);
// My favourite fruits are mango,orange,pineapple,papaya
本文地址:http://javascript.askforanswer.com/meiyuanfuhaohehuakuohaozaijavascriptzifuchuanzhongshishenmeyisi.html
文章标签:concatenation , javascript , string , variables
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
文章标签:concatenation , javascript , string , variables
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
评论已关闭!