在Java中用小括号括起来后,换行符有什么危险? [英] When is it dangerous to line break after a close parenthesis in Javascript?
问题描述
-
在圆括号内将长代码行破坏的危险是什么?
What are the dangers of breaking a line of long code up at a close parenthesis?
何时可以使用Javascript自动插入分号(大概是危险的吧?).
When could a semicolon be automatically inserted by Javascript (presumably that is the danger, right?).
为什么使用)
作为JSLint所皱眉"的换行器?
Why is using a )
as a line breaker "frowned upon" by JSLint?
在Javascript中,有时我会在)
处看到一长行代码,像这样( 示例 ):
In Javascript I sometimes see one long line of code broken up at a )
like this (example):
function ()
{
或这样( 示例 ):
or like this (example):
object.methodOne()
.methodTwo();
但是当阅读 对jsLint的换行期望 时,它说:
But when reading the line break expectations for jsLint, it says:
作为对分号插入机制的进一步防御,JSLint希望仅在以下标点字符或运算符之一之后才中断长语句:
As a further defense against the semicolon insertion mechanism, JSLint expects long statements to be broken only after one of these punctuation characters or operators:
, ; : { } ( [ = < > ? ! + - * / % ~ ^ | &
== != <= >= += -= *= /= %= ^= |= &= << >> || &&
=== !== <<= >>= >>> >>>=
JSLint不会期望在标识符,字符串,数字,更小数或后缀运算符之后出现长语句中断:
JSLint does not expect to see a long statement broken after an identifier, a string, a number, closer, or a suffix operator:
. ) ] ++ --
因此,圆括号被选为JSLint不希望看到的"换行符.
So the close parenthesis is singled out as a line breaker that JSLint "doesn't expect to see."
我更愿意使用
function()
{
因为我发现它更具可读性,并且已经在其他语言中使用它,但是目前我使用:
since I find it more readable, and I already use it in other languages, but currently I use:
function () {
我可以安全地使用)
断开长行吗?
Could I safely use the )
to break up long lines?
推荐答案
此链接应对此进行全部解释:
This link should explain it all:
危险"伴随着(摘自上面的链接,重点已添加):
The "danger" is with (taken from the above link, emphasis added):
语法中有五种限制形式,它们是后缀运算符 ++和-, continue 语句, break 语句, return 语句和 throw 语句.
There are five restricted productions in the grammar, they are the postfix operators ++ and --, continue statements, break statements, return statements, and throw statements.
function()
不在该危险"列表中.但是,在编写分号自由代码时(我不确定这是否是您的目标:-),应该防止以字符开头的行(例如(
或[
)可能会以或继续一个表达式.以下代码显示了可能错误的代码示例:
function()
is not in that "danger" list. However, when writing semi-colon free-code (I'm not sure if this is your aim :-), one should guard against lines starting with characters -- such as (
or [
-- that may start or continue an expression. The following code shows an example of code which is likely wrong:
x()
(function (){...})()
如您所见,使用)
作为换行符可以使表达式能够巧妙地继续 ,而无需在下一行使用明确的分号 iff 可以继续表达.我将程序编写为(如果确实是以下意图):
As you can see, using )
as a line-breaker may make the expression able to continue on subtly without an explicit semi-colon iff the next line can continue the expression. I write the proceeding as (if the following is indeed the intent):
x()
;(function (){...})()
我个人不喜欢JSLint :-) 编码愉快.
Personally, I dislike JSLint :-) Happy coding.
这篇关于在Java中用小括号括起来后,换行符有什么危险?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!