Sze*_*sui 3 javascript string-interpolation ecmascript-6
AKA模板文字,字符串插值.(维基这里.)
它们为什么有用?
我什么时候不应该使用它们?
这些功能与使用JSX和React有何不同?
如何有效地使用String.raw(),如wiki底部所述?
它们使代码更具可读性.例如:
// ES6
let fruit = 'apples'
console.log(`I like ${fruit}.`)
Run Code Online (Sandbox Code Playgroud)
ES5等价物:
// ES5
var fruit = 'apples'
console.log('I like ' + fruit + '.')
Run Code Online (Sandbox Code Playgroud)
多行功能也便于编写大块单词:
// ES6
console.log(`string text line 1
string text line 2`)
Run Code Online (Sandbox Code Playgroud)
ES5等价物:
// ES5
console.log("string text line 1\nstring text line 2")
Run Code Online (Sandbox Code Playgroud)
如果存在变量,则应始终使用模板字符串.