Javascript comma syntax
Why do so many developers write commas this way?
var npm = module.exports = new EventEmitter
, config = require("./lib/config")
, set = require("./lib/utils/set");开发者_如何学运维
Not this way?
var npm = module.exports = new EventEmitter,
config = require("./lib/config"),
set = require("./lib/utils/set");
They write them with the "," at the beginning of the line to make it easier to maintain the code (add lines or remove/comment out lines).
Given this:
var npm = module.exports = new EventEmitter
, config = require("./lib/config")
, set = require("./lib/utils/set");
It's much cleaner and easier to do this:
var npm = module.exports = new EventEmitter
// , config = require("./lib/config")
, set = require("./lib/utils/set");
as well as add new lines like this:
var npm = module.exports = new EventEmitter
, config = require("./lib/config")
, anothervalue = require("./lib/aval")
, anothervalue2 = require("./lib/aval2")
, set = require("./lib/utils/set");
I never saw that pattern before in JS, so I'm not sure if there are that many developers that use it, but I would guess they do that so variable names are aligned, to emphasize that var
actually defines three variables in your sample code.
If that's the case however, it would be clearer (and less weird) to just use var
three times:
var npm = module.exports = new EventEmitter;
var config = require("./lib/config");
var set = require("./lib/utils/set");
This is strictly a programmer-specific syntaxtual preference. I know a lot of DBA's that sem to prefer that method, while I prefer the trailing comma myself. there's nor real difference except for personal preference/education.
var npm = module.exports = new EventEmitter
See the extra "//"... you would have to add the "//" to an existing line at the end as well as add it to the next line...
config = require("./lib/config") // ,
//anothervalue = require("./lib/aval"),
//anothervalue2 = require("./lib/aval2"),
set = require("./lib/utils/set");
EDIT 2: The above question prompted me to scratch an itch that has bothered me for some time. After quite a bit of rumination, I have come up with a good justification for leading commas: easily spot missing commas. This is a potentially nasty source of bugs, since, rather than complain, the compiler will simply add a semicolon, thus changing the meaning quite dramatically:
var a = foo(),
b = bar() // Oops...
c = baz(); // Ouch!
The third line no longer initialises a new variable in the current scope. It assigns to an existing variable in an outer scope or initializes a new global variable if there isn't one.
This doesn't explain the use of leading commas in JSON. Perhaps it just carried over by fiat.
That said, I still favour separate declarations, which make it all simple and safe.
I'll leave my rant in place for posterity.
I agree that it appears all over the place. Almost every second library I peruse uses this convention. Frankly, I don't get it.
Trailing commas make it awkward to comment out the last line of a group...
var //a = foo(), easy
b = bar(),
c = baz();
var a = foo(),
b = bar()/*, awkward
c = baz()*/;
...whereas leading commas make it awkward for both the first and last line...
var /*a = foo() awkward
, */b = bar()
, c = baz();
var a = foo()
, b = bar()/* awkward
, c = baz()*/;
This is progress?
(One could of course win a cheap point by observing that semicolons are optional, but this only achieves parity at the expense of shunning best-practice.)
Worse, leading commas are also used for JSON. Here's a package.json generated by Express.
{
"name": "application-name"
, "version": "0.0.1"
, "private": true
, "dependencies": {
"express": "2.5.10"
, "jade" : ">= 0.0.1"
}
}
This is a PITA because most editors don't like indenting sibling lines differently to each other. Note that the first line of a group is indented more than the following lines.
The use of leading commas strikes me as a pointless affectation that causes problems without solving any. The only argument I can think of that has any merit is that leading commas line up with each other; not very compelling.
Fortunately, CoffeeScript is taking over the world, which renders the question moot (it even trounces JSON for config).
EDIT: I forgot to mention that I don't actually favour trailing commas for the above scenario. I think declaring individual vars makes the most sense. It's clean, regular and makes it very easy to comment out individual items.
var a = foo();
var b = bar();
var c = baz();
精彩评论