在JavaScript中编写多行字符串最简洁的方法是什么? [英] What's the cleanest way to write a multiline string in JavaScript?

查看:152
本文介绍了在JavaScript中编写多行字符串最简洁的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它实际上不需要添加换行符,只是可读的东西。

It doesn't really have to add newlines, just something readable.

还有什么比这更好的吗?

Anything better than this?

str = "line 1" +
      "line 2" +
      "line 3";


推荐答案

几乎与NickFitz的答案相同:

var str = [""
    ,"line 1"
    ,"line 2"
    ,"line 3"
].join("");
// str will contain "line1line2line3"

差异,代码稍微更易于维护因为可以重新排序行而不考虑逗号的位置。没有语法错误。

The difference, the code is slightly more maintainable because the lines can be re-ordered without regard to where the commas are. No syntax errors.

这篇关于在JavaScript中编写多行字符串最简洁的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆