gpt4 book ai didi

javascript - 长单行 ES6 字符串文字

转载 作者:可可西里 更新时间:2023-11-01 01:38:05 25 4
gpt4 key购买 nike

谷歌充满了关于如何从 ES6 字符串文字中获益的博客文章和答案。几乎每一篇深入解释这个特性的博文都有一些关于如何实现多行字符串的细节:

let a = `foo
bar`;

但我找不到任何关于如何实现如下长单行字符串的详细信息:

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}`

有任何线索或解决方法,还是应该改用 es3 字符串?

最佳答案

您可以使用 \ 换行。换行符 (\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}`

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

let a = `Hello\
World`;

a === 'Hello World' // true

关于javascript - 长单行 ES6 字符串文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35427996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com