模板字符串ES6可防止换行

dim*_*_0n 50 javascript ecmascript-6 template-strings

我有一个长字符串,我使用ES6模板字符串构建,但我希望它没有换行符:

var string = `As all string substitutions in Template Strings are JavaScript
              expressions, we can substitute a lot more than variable names.
              For example, below we can use expression interpolation to 
              embed for some readable inline math:`

console.log(string);
Run Code Online (Sandbox Code Playgroud)

结果:

As all string substitutions in Template Strings are JavaScript
expressions, we can substitute a lot more than variable names.
For example, below we can use expression interpolation to
embed for some readable inline math:
Run Code Online (Sandbox Code Playgroud)

我的期望:

As all string substitutions in Template Strings are JavaScript expressions, we can substitute a lot more than variable names. For example, below we can use expression interpolation to embed for some readable inline math:
Run Code Online (Sandbox Code Playgroud)

Cyr*_*PON 62

疯了吧.

这里几乎每一个答案都建议运行一个函数运行时,以便格式化,构建时间格式错误的文本oO我是唯一一个被这个事实震惊的人,尤其是性能影响???

正如@dandavis所说,官方解决方案(其中btw也是unix shell脚本的历史解决方案)是使用espace字符来确定回车符:\

`foo \
bar` === 'foo bar'
Run Code Online (Sandbox Code Playgroud)

这个过程中简单,高效,官方,可读和类似shell

  • 没有运气。 (5认同)
  • 这有效,您正在逃避回车,而不是进一步的空间 (2认同)
  • 不需要“更多空格”或“代码缩进”。“这样做的方法”不是这样做,而是遵循代码标准。您在代码编辑器中看到的常见方式(即自动返回到该行的那些)不是像前一行那样重新缩进,而是转到该行(0 缩进)。如果你真的想缩进它,去尝试一下,无论如何,HTML 只打印一个转义字符。如果你真的不想有 x espace 字符,即使 HTML 自动解析它,用一个工具预处理你的代码,但请不要在运行时运行它,看在上帝的份上 (2认同)

Mat*_*zer 32

换行符是一个换行符...如果你手动生成它们,我发现你很有可能在运行期间得到它们.

顺便说一句,我现在找到三个解决方法:

  1. 配置您的IDE或代码编辑器进行自动换行,这样您就不需要在代码中添加换行符了:如果每个代码语句超出配置的最大值,编辑器将在两行或多行中断开代码字符.

  2. 删除换行符String.prototype.replace:

var string = `As all string substitutions in Template Strings are JavaScript
expressions, we can substitute a lot more than variable names.
For example, below we can use expression interpolation to
embed for some readable inline math:`.replace(/\n/gm,"");

警告:在这里,您正在运行一个函数运行时来格式化您的构建时代码,这可能看起来像一个反模式,并且会影响性​​能

  1. 使用连接执行这些代码换行符:
var string = `As all string substitutions in Template Strings are JavaScript`
              + `expressions, we can substitute a lot more than variable names.`
              + `For example, below we can use expression interpolation to` 
              + `embed for some readable inline math:`;

就我而言,我会选择#1选项.


kvz*_*kvz 21

如果您有ES6,则可以使用标签.例如,common-tags库中的stripIndent标记:

安装途径:

npm install common-tags --save
Run Code Online (Sandbox Code Playgroud)

要求通过:

const stripIndent = require('common-tags/lib/stripIndent')
Run Code Online (Sandbox Code Playgroud)

用于:

stripIndent`
  As all string substitutions in Template Strings are JavaScript
  expressions, we can substitute a lot more than variable names.
  For example, below we can use expression interpolation to
  embed for some readable inline math:        
`
Run Code Online (Sandbox Code Playgroud)

编辑:如评论中所述,您可能需要选择:const oneLine = require('common-tags/lib/oneLine')标记以获得所需的结果.

有关上述common-tags链接以及此博客的更多信息

  • 为库提供+1并提及ES6标签.但是,在这种情况下,我认为OP需要来自`common-tags`库的[`oneLine`](https://github.com/declandewet/common-tags#oneline)标签. (4认同)
  • 运行运行时函数以实现代码格式化... Downvote.虽然lib非常有用,但感谢分享它; 对于这个特殊用例,他声称他"使用es6模板构建字符串",意味着构建时间.性能影响? (2认同)

jay*_*bee 13

我个人更喜欢加入数组的外观:

var string = [
  `As all string substitutions in Template Strings are JavaScript`,
  `expressions, we can substitute a lot more than variable names.`,
  `For example, below we can use expression interpolation to`,
  `embed for some readable inline math:`
].join(' ');
Run Code Online (Sandbox Code Playgroud)

  • 这种方法也比使用 `+` 的连接更快,因为这只会为整个集合创建一个内部 StringBuilder 而不是每个连接一个(尽管这是一个微优化) (2认同)
  • 正在运行用于代码格式化目的的运行时函数。性能影响? (2认同)
  • 换句话说,这更像是我所指的“概念异端”(concept-heresy)本身。从概念上讲,这确实是一种不好的做法,而且人们可以简单地学习使用正确的工具来完成正确的事情。IDE配置为“他们将其视为开发人员”,编译器和预处理器,以使构建时具有目的性,并为它们保留运行时。 (2认同)

Eug*_*lin 10

第一种方法更好,原因是:

  • 更少的符号(尺寸方面)
  • 没有运行时操作(性能方面)

  • 这是最聪明的答案。去接受这个 (4认同)