在 JavaScript 中编写多行字符串最简洁的方法是什么?

发布于 2024-08-08 10:00:05 字数 125 浏览 7 评论 0原文

它实际上不必添加换行符,只需添加可读的内容即可。

还有比这更好的吗?

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

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

Anything better than this?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

枕花眠 2024-08-15 10:00:05

与 NickFitz 的答案几乎相同:

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

不同之处在于,代码的可维护性稍高一些,因为可以对行进行重新排序,而无需考虑逗号在哪里。没有语法错误。

Almost identical to NickFitz's answer:

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.

雨轻弹 2024-08-15 10:00:05

我喜欢这个版本(与您的版本不同,只是代码格式不同):

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

I like this version (different than yours just in formatting of the code):

var str = "line 1"
        + "line 2"
        + "line 3";
拥抱影子 2024-08-15 10:00:05

正如评论中提到

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

的,javascript 解析器可以很好地处理这个问题(它适用于所有主要浏览器),但不是 ECMA 脚本语法的正式部分。因此,它可能会也可能不会与压缩器、错误检查器一起工作,并且不保证在浏览器中工作。

这可能更具可读性,但并不是“最佳”方法。也许有一天 ECMA 脚本会支持像 c# 的 @"" 这样的东西。

You could do

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

As mentioned in the comments, javascript parsers handle this fine (it works in all major browsers), but is not officially part of ECMA Script syntax. As such it may or may not work with compressors, error checkers, and isn't guaranteed to work in browsers.

This may be more readable, but isn't the 'best' way to do it. Maybe ECMA script will support something like c#'s @"" someday.

强者自强 2024-08-15 10:00:05

供参考。
您建议的方式是正确的方式,并且比其他答案更好。 JsLint 仅验证您的版本。

FYI.
The way you suggest it is the correct way and better than the other answers. JsLint only validates your version.

清晰传感 2024-08-15 10:00:05
var str = [
    "line 1",
    "line 2",
    "line 3"
].join("");
// str will contain "line1line2line3"

如果您确实想要在字符串中换行,请将 .join("") 替换为 .join("\n")/

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

If you actually want newlines in the string, then replace .join("") with .join("\n")/

心不设防 2024-08-15 10:00:05

始终如一。

无论您选择哪种方式,请在整个申请过程中以完全相同的方式进行。如果您正在开发一个已经编写了代码的应用程序,请接受他们设置的约定并遵循它。

Consistently.

Whichever way you choose, do it exactly the same way throughout your application. If you're working on an application that already has code written, accept the convention they set and go with it.

要走就滚别墨迹 2024-08-15 10:00:05

是的!您可以使用\ 字符让 JavaScript 忽略行尾字符

str = 'line 1 \
line 2 \
line 3';

然而,正如指出的Elzo Valugi,这不会使用 JSLint 进行验证。

Yes! You can use the \ character to have JavaScript ignore end of line characters.

str = 'line 1 \
line 2 \
line 3';

However, as pointed out by Elzo Valugi, this will not validate using JSLint.

夏尔 2024-08-15 10:00:05

这仅适用于支持 E4X 支持的浏览器 - 我希望我们可以在 IE 中使用它

var str = <><![CDATA[

   Look, a multi-line
   string! < " // ' ? &

]]></>.toString();

This will only work in browsers with E4X support - I wish we could use it in IE

var str = <><![CDATA[

   Look, a multi-line
   string! < " // ' ? &

]]></>.toString();
甜扑 2024-08-15 10:00:05

以下是在使用 Chrome 进行开发时可能会有所帮助的一项内容。

function FSTR(f) {
    // remove up to comment start and trailing spaces and one newline
    s = f.toString().replace(/^.*\/\* *\r?\n/,"");
    // remove the trailing */} with preceeding spaces and newline
    s = s.replace(/\n *\*\/\s*\}\s*$/,"")
    return s;
}

s = FSTR(function(){/*
uniform vec2 resolution;
uniform float time;

void main(void)
{
    vec2 p = -1.0 + 2.0 * gl_FragCoord.xy / resolution.xy;
    vec2 cc = vec2( cos(.25*time), sin(.25*time*1.423) );
    ...
    float color = sqrt(sqrt(dmin))*0.7;
    gl_FragColor = vec4(color,color,color,1.0);
}
*/});

尽管它可以在 Chrome 中使用,但它不适用于 Firefox

示例用途是编写/测试 webgl 着色器。
在开发过程中,使用它会更好
稍后您可以随时使用简单的正则表达式来运行它
将该语法转换为跨浏览器版本。

Here's one that may be helpful during development when using Chrome.

function FSTR(f) {
    // remove up to comment start and trailing spaces and one newline
    s = f.toString().replace(/^.*\/\* *\r?\n/,"");
    // remove the trailing */} with preceeding spaces and newline
    s = s.replace(/\n *\*\/\s*\}\s*$/,"")
    return s;
}

s = FSTR(function(){/*
uniform vec2 resolution;
uniform float time;

void main(void)
{
    vec2 p = -1.0 + 2.0 * gl_FragCoord.xy / resolution.xy;
    vec2 cc = vec2( cos(.25*time), sin(.25*time*1.423) );
    ...
    float color = sqrt(sqrt(dmin))*0.7;
    gl_FragColor = vec4(color,color,color,1.0);
}
*/});

This does not work for Firefox, although it works in Chrome.

Example use would be for writing/testing webgl shaders.
During development this is much nicer to work with and
later you can always run over this with a simple regexp
that converts that syntax into a cross-browser version.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文