The JavaScript Comma Operator
javascriptweblog.wordpress.com
javascriptweblog.wordpress.com
That's a curious way to find programmers that are going to write code that you plan to use in production
lhs && rhs
lhs || rhs
lhs , rhs
Given the way js evaluates these, the comma operator seems like it should fit in nicely. I would have made this the first argument in my case for using the comma.Also the way it allows one to formulate a `do { .. } while ( .. )` construction, is a nice and clever trick, but already violating the principle of least astonishment too much.
var foo = bar ? a : b; var x = 1,
y = 2;
Destructuring lists would be a much more useful application var x, y = [1, 2];
or var tmp = [1, 2]
...
var x, y = tmp;
would be far more useful.(though I kinda feel like panning syntax sugar as 'useless' is a little useless. What, are we going to count chars as a scientific comparison?)
http://wiki.ecmascript.org/doku.php?id=harmony:destructuring
I've screwed myself over a few times by getting into the habit of using the comma in this fashion. If you accidentally make that comma a semicolon, y in your example becomes global. My jQuery plugins all include a default settings object, and they happen to have the same name of "settings". Consequently, default settings for some plugins were being overridden and I had no idea why - until I found that I used a semicolon instead of a comma.
var a = (1, b = 2, c = 3);
You would be using the comma operator. But that would be stupid.It's the same operation as in:
for (a = 0, b = 1; ... ; ...)