使用逗号分配是否有效?

时间:2022-06-28 01:36:02

Why does aaa = 1,2,3 work and set the value of aaa to 1?

为什么aaa = 1,2,3可以工作并将aaa的值设置为1?

Why doesn't var bbb = 1,2,3 work?

为什么var bbb = 1,2,3不起作用?

Why does var bbb = (1,2,3) work and set the value of bbb to 3?

为什么var bbb =(1,2,3)工作并将bbb的值设置为3?

使用逗号分配是否有效?

4 个解决方案

#1


199  

There's a lot going on here, but basically, it comes down to the comma operator.

这里有很多内容,但基本上,它归结为逗号运算符。

The comma operator evaluates both of its operands (from left to right) and returns the value of the second operand.

逗号运算符计算其两个操作数(从左到右)并返回第二个操作数的值。


This code:

这段代码:

aaa = 1,2,3

Is equivalent to:

相当于:

aaa = 1;
2;
3;

So aaa is implicitly declared and assigned a value of 1. Notice that the output on the console is the result of the last statement, 3.

因此,aaa被隐式声明并赋值为1.请注意,控制台上的输出是最后一个语句的结果,3。


This code:

这段代码:

var bbb = 1,2,3

Is a syntax error because commas in variable declarations are used to declare multiple variables in a single line. As the MDN article points out,

是语法错误,因为变量声明中的逗号用于在一行中声明多个变量。正如MDN文章指出的那样,

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.

请注意,var语句中的逗号不是逗号运算符,因为它在表达式中不存在。相反,它是var语句中的一个特殊字符,可将多个组合成一个。

So this code is roughly equivalent to:

所以这段代码大致相当于:

var bbb = 1;
var 2;
var 3;

Of course, 2 is not a valid identifier, so it fails at that point.

当然,2不是有效的标识符,因此它在那时失败。


This code:

这段代码:

var bbb = (1,2,3)

Is very similar to the first, except because the numeric values are wrapped in a parentheses, they are evaluated first. So this is rougly equivalent to:

与第一个非常相似,除了因为数值包含在括号中,它们首先被计算。所以这相当于:

1;
2;
var bbb = 3;

#2


9  

Comma has multiple uses in Javascript. In the expression:

逗号在Javascript中有多种用途。在表达式中:

a = 1, 2, 3;

it's an operator that simply returns its right-hand argument. But it's also part of the syntax of var declarations, which are:

它是一个简单地返回其右手参数的运算符。但它也是var声明语法的一部分,它们是:

var var1 [ = val1 ], var2 [ = val2 ], var3 [ = val3 ], ...;

(where [...] means that part is optional). Your var declaration is missing the variable names after the commas, so it doesn't parse. You could get the effect you wanted with:

(其中[...]表示该部分是可选的)。您的var声明在逗号后面缺少变量名,因此它不会解析。你可以得到你想要的效果:

var a = (1, 2, 3);

The parentheses force the commas to be treated as operators rather than delimiters between variable declarations.

括号强制将逗号视为运算符而不是变量声明之间的分隔符。

#3


7  

In your examples, the comma is used in two contexts:

在您的示例中,逗号用于两个上下文:

var statement

The syntax of var statement is:

var语句的语法是:

var varname1 [= value1 [, varname2 [, varname3 ... [, varnameN]]]];

Here, comma is used to separate variable name-value pairs. The following will not work because a variable name cannot start with a digit (see identifier names):

这里,逗号用于分隔变量名称 - 值对。以下操作无效,因为变量名不能以数字开头(请参阅标识符名称):

var bbb = 1, 2, 3;
// SyntaxError: Unexpected number

Comma Operator

The comma operator evaluates both of its operands (from left to right) and returns the value of the second operand. The following expressions work as follows:

逗号运算符计算其两个操作数(从左到右)并返回第二个操作数的值。以下表达式的工作方式如下:

aaa = 1, 2, 3;
  • aaa = 1, 2 yields 2
    • note that aaa = 1 is evaluated first because = has higher priority than ,
    • 请注意首先评估aaa = 1,因为=具有更高的优先级,
  • aaa = 1,2产生2注意首先评估aaa = 1,因为=具有更高的优先级,
  • 2, 3 yields 3
  • 2,3收益率3
var bbb = (1, 2, 3);
  • the expression (1, 2, 3) yields 3 as described above
  • 表达式(1,2,3)如上所述产生3
  • The variable bbb is assigned the value 3
  • 变量bbb的值为3

#4


1  

In the first case:

在第一种情况下:

aaa = 1,2,3

aaa = 1,2,3

the commas serve as expression separators. It performs an assignment to aaa, then it calculates 2 and discards it, then it calculates 3 and discards it.

逗号用作表达式分隔符。它执行aaa的赋值,然后计算2并丢弃它,然后计算3并丢弃它。

In the second:

在第二:

var bbb = 1,2,3

The var keyword tells the Javascript compiler that the next thing after a , should be another variable name. It isn't finding on, so it dies and gags.

var关键字告诉Javascript编译器a之后的下一个应该是另一个变量名。它没有找到,所以它死了,噱头。

var bbb = (1,2,3)

Here, the compiler first evaluates 1 and ignores it. Then it evaluates 2 and ignores it. Then it evaluates 3 and that is left on the stack so it is assigned to bbb

这里,编译器首先计算1并忽略它。然后它评估2并忽略它。然后它评估3,它留在堆栈上,因此它被分配给bbb

While using commas to separate expressions isn't common, it is sometimes useful in things like for looks.

虽然使用逗号分隔表达式并不常见,但它有时在外观方面很有用。

for (i = 0, l = 10; i < l; i++) {
  console.log(i);
}

#1


199  

There's a lot going on here, but basically, it comes down to the comma operator.

这里有很多内容,但基本上,它归结为逗号运算符。

The comma operator evaluates both of its operands (from left to right) and returns the value of the second operand.

逗号运算符计算其两个操作数(从左到右)并返回第二个操作数的值。


This code:

这段代码:

aaa = 1,2,3

Is equivalent to:

相当于:

aaa = 1;
2;
3;

So aaa is implicitly declared and assigned a value of 1. Notice that the output on the console is the result of the last statement, 3.

因此,aaa被隐式声明并赋值为1.请注意,控制台上的输出是最后一个语句的结果,3。


This code:

这段代码:

var bbb = 1,2,3

Is a syntax error because commas in variable declarations are used to declare multiple variables in a single line. As the MDN article points out,

是语法错误,因为变量声明中的逗号用于在一行中声明多个变量。正如MDN文章指出的那样,

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.

请注意,var语句中的逗号不是逗号运算符,因为它在表达式中不存在。相反,它是var语句中的一个特殊字符,可将多个组合成一个。

So this code is roughly equivalent to:

所以这段代码大致相当于:

var bbb = 1;
var 2;
var 3;

Of course, 2 is not a valid identifier, so it fails at that point.

当然,2不是有效的标识符,因此它在那时失败。


This code:

这段代码:

var bbb = (1,2,3)

Is very similar to the first, except because the numeric values are wrapped in a parentheses, they are evaluated first. So this is rougly equivalent to:

与第一个非常相似,除了因为数值包含在括号中,它们首先被计算。所以这相当于:

1;
2;
var bbb = 3;

#2


9  

Comma has multiple uses in Javascript. In the expression:

逗号在Javascript中有多种用途。在表达式中:

a = 1, 2, 3;

it's an operator that simply returns its right-hand argument. But it's also part of the syntax of var declarations, which are:

它是一个简单地返回其右手参数的运算符。但它也是var声明语法的一部分,它们是:

var var1 [ = val1 ], var2 [ = val2 ], var3 [ = val3 ], ...;

(where [...] means that part is optional). Your var declaration is missing the variable names after the commas, so it doesn't parse. You could get the effect you wanted with:

(其中[...]表示该部分是可选的)。您的var声明在逗号后面缺少变量名,因此它不会解析。你可以得到你想要的效果:

var a = (1, 2, 3);

The parentheses force the commas to be treated as operators rather than delimiters between variable declarations.

括号强制将逗号视为运算符而不是变量声明之间的分隔符。

#3


7  

In your examples, the comma is used in two contexts:

在您的示例中,逗号用于两个上下文:

var statement

The syntax of var statement is:

var语句的语法是:

var varname1 [= value1 [, varname2 [, varname3 ... [, varnameN]]]];

Here, comma is used to separate variable name-value pairs. The following will not work because a variable name cannot start with a digit (see identifier names):

这里,逗号用于分隔变量名称 - 值对。以下操作无效,因为变量名不能以数字开头(请参阅标识符名称):

var bbb = 1, 2, 3;
// SyntaxError: Unexpected number

Comma Operator

The comma operator evaluates both of its operands (from left to right) and returns the value of the second operand. The following expressions work as follows:

逗号运算符计算其两个操作数(从左到右)并返回第二个操作数的值。以下表达式的工作方式如下:

aaa = 1, 2, 3;
  • aaa = 1, 2 yields 2
    • note that aaa = 1 is evaluated first because = has higher priority than ,
    • 请注意首先评估aaa = 1,因为=具有更高的优先级,
  • aaa = 1,2产生2注意首先评估aaa = 1,因为=具有更高的优先级,
  • 2, 3 yields 3
  • 2,3收益率3
var bbb = (1, 2, 3);
  • the expression (1, 2, 3) yields 3 as described above
  • 表达式(1,2,3)如上所述产生3
  • The variable bbb is assigned the value 3
  • 变量bbb的值为3

#4


1  

In the first case:

在第一种情况下:

aaa = 1,2,3

aaa = 1,2,3

the commas serve as expression separators. It performs an assignment to aaa, then it calculates 2 and discards it, then it calculates 3 and discards it.

逗号用作表达式分隔符。它执行aaa的赋值,然后计算2并丢弃它,然后计算3并丢弃它。

In the second:

在第二:

var bbb = 1,2,3

The var keyword tells the Javascript compiler that the next thing after a , should be another variable name. It isn't finding on, so it dies and gags.

var关键字告诉Javascript编译器a之后的下一个应该是另一个变量名。它没有找到,所以它死了,噱头。

var bbb = (1,2,3)

Here, the compiler first evaluates 1 and ignores it. Then it evaluates 2 and ignores it. Then it evaluates 3 and that is left on the stack so it is assigned to bbb

这里,编译器首先计算1并忽略它。然后它评估2并忽略它。然后它评估3,它留在堆栈上,因此它被分配给bbb

While using commas to separate expressions isn't common, it is sometimes useful in things like for looks.

虽然使用逗号分隔表达式并不常见,但它有时在外观方面很有用。

for (i = 0, l = 10; i < l; i++) {
  console.log(i);
}