长,单行ES6字符串文字

can*_*eel 14 javascript ecmascript-6

谷歌充满了博客文章和答案,质疑如何从ES6字符串文字中受益.几乎所有博客文章都深入解释了这一功能,并提供了有关如何实现多行字符串的一些细节:

let a = `foo
bar`;
Run Code Online (Sandbox Code Playgroud)

但我找不到有关如何实现如下所示的长单行字符串的任何细节:

let a = `This is a very long single line string which might be used to display assertion messages or some text. It has much more than 80 symbols so it would take more then one screen in your text editor to view it. Hello ${world}`
Run Code Online (Sandbox Code Playgroud)

任何线索或变通方法,还是应该坚持使用es3字符串?

Gio*_*Gpx 25

您可以使用字符串继续换行\.换行符(\n \n)不会出现在字符串本身中.

let a = `This is a very long single line string which might be used \
to display assertion messages or some text. It has much more than \
80 symbols so it would take more then one screen in your text \
editor to view it. Hello ${world}`
Run Code Online (Sandbox Code Playgroud)

注意不要缩进你的字符串,否则缩进将在字符串中:

let a = `Hello\
         World`;

a === 'Hello         World' // true
Run Code Online (Sandbox Code Playgroud)