如何在JavaScript中的多行代码中断字符串? [英] How do I break a string across more than one line of code in JavaScript?

查看:110
本文介绍了如何在JavaScript中的多行代码中断字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript中是否有一个字符可以分解一行代码,这样即使在新行上它也会被连续读取?

Is there a character in JavaScript to break up a line of code so that it is read as continuous despite being on a new line?

类似...... 。


1. alert ( "Please Select file   
2. \ to delete" );


推荐答案

在您的示例中,您可以将字符串分成两部分:

In your example, you can break the string into two pieces:

alert ( "Please Select file"
 + " to delete");

或者,当它是一个字符串时,就像你的情况一样,你可以使用反斜杠

Or, when it's a string, as in your case, you can use a backslash as @Gumbo suggested:

alert ( "Please Select file\
 to delete");

请注意,这种反斜杠方法是不一定是首选,可能不受普遍支持(我无法在此找到硬数据)。 ECMA 5.1中的规范

Note that this backslash approach is not necessarily preferred, and possibly not universally supported (I had trouble finding hard data on this). It is not in the ECMA 5.1 spec.

使用其他代码(不在引号中)时,会忽略换行符,并且完全可以接受。例如:

When working with other code (not in quotes), line breaks are ignored, and perfectly acceptable. For example:

if(SuperLongConditionWhyIsThisSoLong
  && SuperLongConditionOnAnotherLine
  && SuperLongConditionOnThirdLineSheesh)
{
    // launch_missiles();
}

这篇关于如何在JavaScript中的多行代码中断字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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