最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript comma syntax - Stack Overflow

programmeradmin2浏览0评论

Why do so many developers write mas 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");

Why do so many developers write mas 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");
Share Improve this question edited May 14, 2016 at 23:50 Jonathan Eustace 2,48713 gold badges34 silver badges54 bronze badges asked Jan 14, 2011 at 20:15 IchoozIchooz 634 bronze badges 2
  • 1 Many? Do you have any concrete stats/evidence? – BalusC Commented Jan 14, 2011 at 20:18
  • 4 @BalusC: Does s/he need "concrete stats" to ask such a question? Anyone who's spent a couple months in JS world has seen this pattern somewhere, in a library or in a coworker's code. – Ben Zotto Commented Jan 14, 2011 at 20:24
Add a ment  | 

5 Answers 5

Reset to default 7

They write them with the "," at the beginning of the line to make it easier to maintain the code (add lines or remove/ment 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 ma myself. there's nor real difference except for personal preference/education.

var npm = module.exports = new EventEmitter
config = require("./lib/config") // ,
//anothervalue = require("./lib/aval"),
//anothervalue2 = require("./lib/aval2"),
set = require("./lib/utils/set");
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...

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 e up with a good justification for leading mas: easily spot missing mas. This is a potentially nasty source of bugs, since, rather than plain, the piler 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 mas 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 mas make it awkward to ment 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 mas 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 mas 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 mas 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 mas line up with each other; not very pelling.

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 mas for the above scenario. I think declaring individual vars makes the most sense. It's clean, regular and makes it very easy to ment out individual items.

var a = foo();
var b = bar();
var c = baz();
发布评论

评论列表(0)

  1. 暂无评论