简体   繁体   中英

What does a comma do in assignment statements in JavaScript?

I found this in a piece of code and i'm wondering what it does? Assign b to x... but what's with the ,c ?

var x = b, c;

That declares two variables, x and c , and assigns value b to variable x .

This is equivalent to the more explicit form * :

var x = b;
var c;

JavaScript allows multiple declarations per var keyword – each new variable is separated by a comma. This is the style suggested by JSLint, which instructs developers to use a single var per function (the error message from JSLint is Combine this with the previous 'var' statement. ).

* Actually, due to hoisting it will be interpreted as var x; var c; x = b var x; var c; x = b var x; var c; x = b .

这定义了两个局部变量xc - 同时将x的值设置为等于b的值。

c is undefined .

This is equivalent:

var x = b;
var c;

It's the same as

var x = b;
var c; 

One of those so clever it's extremely stupid additions to a language.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM