ES6提供了一种使用反向标记作为分隔符的新类型的字符串文本。 这些文本可以嵌入基本的字符串插值表达式,并自动分析和评估。
let person={ name : ' rajinikanth ',age:68,greeting:'Thalaivaaaa!' (;
let usualHtmlStr='
My name is ' person.name ',
n' ' I am ' person.age ' old
n' ' ""+ person.greeting +"" is what I usually say'; let newHtmlStr=
`
My name is ${person.name},
I am ${person.age} old
"${person.greeting}" is what I usually say`;
console.log(usualhtmlstr;
console.log(newhtmlstr; 如您所见,我们在一系列字符周围使用了`。 虽然这些字符被解释为字符串文字,但${.}格式的任何表达式都会立即进行内联分析和评估。 对字符串文字进行插值的一个非常好的优点是可以将其拆分为多行。
varactor={ ' name ' : ' rajinikanth ' };
vartext=` nowisthetimeforallgoodmenlike $ { actor.name } tocometotheaidoftheircountry! `;
console.log(Text;
//nowisthetimeforallgoodmentocometotheaidoftheircountry!
使用ES6 :
字符串可以跨越多行。
不需要转义引号中的字符。
可以避免分组: "
不需要加号运算符。