JavaScript 中的换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10514873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Line continuation characters in JavaScript
提问by xralf
What is the best practice for line continuation in JavaScript? I know that you can use \
for strings. But how would you split the following code?
JavaScript 中换行的最佳实践是什么?我知道你可以\
用于字符串。但是您将如何拆分以下代码?
var statement = con.createStatement("select * from t where
(t.a1 = 0 and t.a2 >=-1)
order by a3 desc limit 1");
回答by Fabrizio Calderan
If I properly understood your question:
如果我正确理解你的问题:
var statement = con.createStatement('select * from t where '
+ '(t.a1 = 0 and t.a2 >=-1) '
+ 'order by a3 desc limit 1');
For readability, it is fine to align +
operator on each row:
Anyway, unless you're using Ecmascript 2015, avoid to split a multiline string with \
, because:
为了可读性,可以+
在每一行上对齐运算符:无论如何,除非您使用 Ecmascript 2015,否则避免使用 分割多行字符串\
,因为:
- It's not standard JavaScript
- A whitespace after that character could generate a parsing error
- 它不是标准的 JavaScript
- 该字符后的空格可能会产生解析错误
回答by user1477388
I like using backslashes for JavaScript line continuation, like so:
我喜欢在 JavaScript 行延续中使用反斜杠,如下所示:
// validation
$(".adjustment, .info input, .includesAndTiming input, \
.independentAdj, .generalAdj, .executiveAdj \
#officeExpense, #longDistanceExpense, #digitalImages, #milesReimbursment, #driveTime, #statementTranscription").keypress(function (event) {
回答by TDHofstetter
My personal preference is similar to your first response there, but for my eyes its readability is easier:
我的个人偏好与您在那里的第一反应相似,但在我看来,它的可读性更容易:
var statement = con.createStatement
(
'select * from t where ' +
'(t.a1 = 0 and t.a2 >=-1) ' +
'order by a3 desc limit 1'
);
It strikes a close similarity to the SQL syntax format I've been using for nearly 20 years:
它与我使用了近 20 年的 SQL 语法格式非常相似:
SELECT *
FROM t
WHERE
t.a1 = 0 AND
t.a2 >=-1
ORDER BY a3 DESC
LIMIT 1
Keeping the continuation (+
in JavaScript or AND
in SQL) on the far right permits the eye to slide evenly down the left edge, checking lvalues & syntax. That's slightly more difficult to do with the continuation on the left - not important unless you do a LOT of this stuff, at which point every calorie you spend is a calorie that might've been saved by a slight improvement in format.
保持最右侧的延续(+
在 JavaScript 或AND
SQL 中)允许眼睛均匀地向下滑动左边缘,检查左值和语法。左边的延续稍微有点困难 - 除非你做了很多这样的事情,否则这并不重要,在这一点上,你花费的每一卡路里都是可能通过稍微改进格式而节省的卡路里。
Since this query is so simple, breaking it all out to SQL format is wasteful of space and bandwidth, which is why the suggested JavaScript is on six lines instead of ten. Collapsing the curlies up one line each brings it to four lines, saving whitespace. Not quite as clear or as simple to edit, though.
由于这个查询非常简单,将其全部分解为 SQL 格式会浪费空间和带宽,这就是为什么建议的 JavaScript 是 6 行而不是 10 行。将卷曲折叠成一行,每行将其变为四行,从而节省空白。不过,编辑起来并不那么清晰或简单。
回答by ThomasJ
The "+" is for concatenation of strings and most of the examples are dealing with strings. What if you have a command you need to string across multiple lines, such as a compound "if" statement? You need a backslash at the end of each line that is to be continued. This escapes the invisible next line character so that it will not delimit the command in mid statement.
“+”用于连接字符串,大多数示例都在处理字符串。如果您有一个需要跨多行字符串的命令,例如复合“if”语句,该怎么办?您需要在要继续的每一行的末尾加一个反斜杠。这会转义不可见的下一行字符,以便它不会在 mid 语句中分隔命令。