- 字符串格式化,使用 ${} 进行格式化。
示例1:
const s = 'hello';
console.log(`say ${s}`); //执行结果 say hello
示例2:
//旧代码,需要用 str.concat()进行字符串拼接
let imageSource = '';
let str1 =
'http://file/download?fileName=';
let str2 = '&user=e';
imageSource = str1.concat(imageCode, str2);
//新代码,使用${}只要一行就可以,注意这里是反引号
const imageSource = `http://file/download?fileName=${imageCode}&user=e`;
- 字符串换行,将需要换行的字符串用反引号``包裹起来即可。
console.log(`say
hello
wolrd`);
运行结果:
say
hello
wolrd