在JavaScript中使用逗号与分号? [英] Use of commas versus semicolons in JavaScript?

查看:210
本文介绍了在JavaScript中使用逗号与分号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下代码

  var fn = function(){
var x ='x',
y ='y';
this.a ='a',
this.b ='b',
this.c ='c';
this.d ='d',
this.e ='e';
}

你可以看到两者兼而有之。

使用其中一个有什么好处?



我的理解是分号是结束语句。并且逗号应该用于将多个声明串在一起。



所以说这个例子那么应该只有两个分号吗?

  var fn = function(){
var x ='x',
y ='y';
this.a ='a',
this.b ='b',
this.c ='c',
this.d ='d',
this.e ='e';
}


解决方案

逗号运算符是运算符可以在表达式中使用。它用于分离多个不同的表达式,其含义是计算以下所有表达式,然后生成最终表达式的值。例如:

  a = 1,b = 2,c = 3 

表示评估 a = 1 ,然后 b = 2 ,然后 c = 3 ,然后计算表达式的值 c = 3



分号不是运算符,不能在表达式中使用。它用作JavaScript语法的一部分,用于标记被视为语句的表达式的结尾。例如,你可以说

  a = 1; b = 2; c = 3; 

这意味着按顺序执行三个语句:将第一个表达式计算为第一个语句,将第二个表达式计算为第二个表达式语句,第三个表达式作为第三个语句。



在这方面,两者不是完全可以互换的。例如,你不能写

  var a = 1,var b = 2; 

因为 var a = 1 var b = 2 是语句,而不是表达式,因此不能用逗号分隔。你必须在这里使用分号。



(注意:你可以说

  var a = 1 ,b = 2; 

因为该语言特别允许使用逗号作为语法的一部分声明声明。这里,逗号不用作运算符。)



同样,你不能说

  a =(b = 1; c = 2); 

因为这里表达式的右边必须是表达式,而不是语句,而; 用于分隔语句。内部分号必须是逗号。 (然后,这个代码首先非常尴尬和不寻常,所以你可能根本不应该这样做!)



从文体的角度来看,逗号运算符很少被使用,并且模糊不清以至于它可能会使合理的JavaScript编码器绊倒。因此,我强烈建议不要使用它,而是遵循JavaScript中关于使用分号来终止语句的既定约定,即使使用逗号分隔出每个用作语句的表达式在语法上也是合法的。 / p>

希望这会有所帮助!


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';   
}

You can see that 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';   
}

解决方案

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.

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

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.

(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.

Hope this helps!

这篇关于在JavaScript中使用逗号与分号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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