javascript 使用逗号还是分号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15485735/
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
Use of commas versus semicolons?
提问by The_asMan
Given the following code:
鉴于以下代码:
var fn = function () {
var x = 'x',
y = 'y';
this.a = 'a',
this.b = 'b',
this.c = 'c';
this.d = 'd',
this.e = 'e';
}
As can be seen, there is a mix of both.
可以看出,两者兼而有之。
What would be the benefit of using one or the other?
使用其中之一有什么好处?
My understanding is that the semicolon is to end the statement. And comma should be used to string together multiple declarations.
我的理解是分号是结束语句。应使用逗号将多个声明串在一起。
So is it safe to say that with this example then there should only be two semicolon?
那么可以肯定地说,在这个例子中应该只有两个分号吗?
var fn = function () {
var x = 'x',
y = 'y';
this.a = 'a',
this.b = 'b',
this.c = 'c',
this.d = 'd',
this.e = 'e';
}
回答by templatetypedef
The comma operator is an operator that can be used inside an expression. It is used to separate out multiple different expressions and has the meaning "evaluate all of the following expressions, then produce the value of the final expression." For example:
逗号运算符是可以在表达式中使用的运算符。用于分离出多个不同的表达式,意思是“对以下所有表达式求值,然后产生最终表达式的值”。例如:
a = 1, b = 2, c = 3
means "evaluate a = 1
, then b = 2
, then c = 3
, then evaluate to the value of the expression c = 3
.
表示“求值a = 1
,然后b = 2
,然后c = 3
,然后求值到表达式的值c = 3
。
The semicolon is not an operator and cannot be used inside an expression. It is used as part of JavaScript syntax to mark the end of an expression that is being treated as a statement. For example, you could say
分号不是运算符,不能在表达式中使用。它用作 JavaScript 语法的一部分,用于标记被视为语句的表达式的结尾。例如,你可以说
a = 1; b = 2; c = 3;
And this would mean "there are three statements to do in sequence: evaluate the first expression as the first statement, the second expression as the second statement, and the third expression as the third statement."
这意味着“依次执行三个语句:将第一个表达式计算为第一个语句,将第二个表达式计算为第二个语句,将第三个表达式计算为第三个语句。”
In this regard, the two are not completely interchangeable. For example, you cannot write
在这方面,两者并不完全可以互换。例如,你不能写
var a = 1, var b = 2;
Because var a = 1
and var b = 2
are statements, not expressions, and thus can't be separated by commas. You would have to use a semicolon here.
因为var a = 1
andvar b = 2
是语句,而不是表达式,因此不能用逗号分隔。您必须在此处使用分号。
(A note: you could say
(注:你可以说
var a = 1, b = 2;
because the language specifically permits this use of comma as a part of the syntax of a declaration statement. Here, comma is not used as an operator.)
因为该语言特别允许将逗号用作声明语句语法的一部分。在这里,逗号不用作运算符。)
Similarly, you can't say
同样,你不能说
a = (b = 1; c = 2);
Because here the right-hand side of the expression must be an expression, not a statement, and ;
is used to separate statements. The inner semicolon would have to be a comma instead. (Then again, this code is pretty awkward and unusual in the first place, so you probably shouldn't do this at all!)
因为这里表达式的右边必须是表达式,而不是语句,;
用于分隔语句。内部分号必须是逗号。(话又说回来,这个代码一开始就非常尴尬和不寻常,所以你可能根本不应该这样做!)
From a stylistic perspective, the comma operator is rarely used and is obscure enough that it might trip up reasonably competent JavaScript coders. As a result, I would strongly suggest not using it and instead following the established conventions in JavaScript about using semicolons to terminate statements, even if it would be equivalent and syntactically legal to use commas to separate out expressions that are each used as statements.
从文体的角度来看,逗号运算符很少使用,而且很模糊,可能会绊倒相当称职的 JavaScript 编码人员。因此,我强烈建议不要使用它,而是遵循 JavaScript 中关于使用分号终止语句的既定约定,即使使用逗号分隔每个用作语句的表达式是等效且在语法上合法的。
Hope this helps!
希望这可以帮助!
回答by gdoron is supporting Monica
No, comma has three meanings.
不,逗号有三个含义。
- With variable declartions, it just let you omit the
var
before each variable. - With expressions, "The comma operator evaluates both of its operands (from left to right) and returns the value of the second operand."
- Separate arguments in function declarations and function calls, but that should be obvious to every programmer.
- 对于变量声明,它只是让您省略
var
每个变量之前的内容。 - 对于表达式,“逗号运算符计算其两个操作数(从左到右)并返回第二个操作数的值。”
- 将函数声明和函数调用中的参数分开,但这对每个程序员来说都应该是显而易见的。
Example for two;
两个例子;
alert((2,3)); //3
Note that the comma in the var statement is not the comma operator, because it doesn't exist within an expression. Rather, it is a special character in var statements to combine multiple of them into one. Practically, that comma behaves almost the same as the comma operator, though.
请注意,var 语句中的逗号不是逗号运算符,因为它不存在于表达式中。相反,它是 var 语句中的一个特殊字符,可以将它们组合成一个。实际上,该逗号的行为与逗号运算符几乎相同。
回答by Jivings
It really doesn't matter. There's no speed benefit to using commas as far as I know.
真的没关系。据我所知,使用逗号没有速度上的好处。
Just use whatever you prefer :)
只需使用您喜欢的任何东西:)
This is what JavaScript - The Good Partshas to say:
这就是JavaScript - The Good Parts必须说的:
The comma operator can lead to excessively tricky expressions. It can also mask some programming errors.
JSLint expects to see the comma used as a separator, but not as an operator (except in the initialization and incrementation parts of the for statement). It does not expect to see elided elements in array literals. Extra commas should not be used. A comma should not appear after the last element of an array literal or object literal because it can be misinterpreted by some browsers.
逗号运算符可能会导致过于棘手的表达式。它还可以掩盖一些编程错误。
JSLint 希望看到逗号用作分隔符,而不是用作运算符(for 语句的初始化和增量部分除外)。它不希望在数组文字中看到被省略的元素。不应使用额外的逗号。逗号不应该出现在数组字面量或对象字面量的最后一个元素之后,因为它可能会被某些浏览器误解。
回答by Mohamad Ali Baydoun
It's best to always remain consistent with convention. It's either all semi-colons or all commas, and I'm sure you'd prefer to use semi-colons rather than commas everywhere.
最好始终与惯例保持一致。要么全是分号,要么全是逗号,我敢肯定您更喜欢在任何地方使用分号而不是逗号。
There is no speed gain as well, so there's nothing to worry about.
也没有速度增益,所以没有什么可担心的。