简体   繁体   English

为什么要关心 Node.js 中的尾随逗号?

[英]Why care about trailing commas in Node.js?

In most Node.js libraries people take special care removing trailing commas after the last key-property pair of objects:在大多数 Node.js 库中,人们特别注意删除最后一对键属性对象后的尾随逗号:

var test = {
    key1: 123,
    key2: 456,
    key3: 789
};

This produces some troubles while editing the code, eg to swap last two key-value pairs one has also to add one comma and to remove one.这在编辑代码时会产生一些麻烦,例如交换最后两个键值对还必须添加一个逗号并删除一个。 Some people move commas to the next line, which solves the issue with the last element but also makes the code a bit harder to read (IMHO):有些人将逗号移到下一行,这解决了最后一个元素的问题,但也使代码更难阅读(恕我直言):

var test = {
      key1: 123
    , key2: 456
    , key3: 789
};

On the other hand as far as I know the trailing commas in JavaScript produce troubles only in some IE browsers.另一方面,据我所知,JavaScript 中的尾随逗号只会在某些 IE 浏览器中产生问题。 So I'm wondering are there any technical reasons not to write hashes with trailing commas in Node.js?所以我想知道是否有任何技术原因不能在 Node.js 中用尾随逗号编写哈希? (Like the following:) (如下:)

var test = {
    key1: 123,
    key2: 456,
    key3: 789,        
};

No, there is no technical reason to do that.不,没有技术上的理由这样做。

However, I never put trailing comas just because I think it makes for cleaner code. 但是,我从不放置尾随逗号,因为我认为它可以使代码更清晰。 Probably some also have the habit coming from web development where, like you mentioned, you need to be careful about those because of IE. 可能有些人也有来自Web开发的习惯,就像你提到的,你需要小心那些因为IE。

Edit: This answer made sense back in 2012, but today, with major browser support and tools like Babel for older browsers, I think trailing commas should be the default for everyone.编辑:这个答案在 2012 年是有道理的,但是今天,有了主要的浏览器支持和 Babel 等用于旧浏览器的工具,我认为尾随逗号应该是每个人的默认设置。 The benefits are that it makes adding a new line easier, and the relevant Git diff is cleaner.好处是它使添加新行更容易,并且相关的 Git 差异更清晰。

Update: I've changed my position on this.更新:我已经改变了我的立场。 I think trailing commas are great because you don't have to change two lines to add a property, and the diff shows only the added line.我认为尾随逗号很棒,因为您不必更改两行即可添加属性,并且差异仅显示添加的行。

Old and wrong:旧的和错误的:

I found a great reason to not use trailing commas , but it's not specific to node.js :我找到了一个不使用尾随逗号很好的理由但它不是特定于 node.js 的

From Johan Sundström : 来自 Johan Sundström

The benefit of this format is diff friendliness: adding or removing a property or array member almost never touches any other lines but the one where a change actually happened, whereas trailing-comma syntax almost always touches two lines to get the commas right.这种格式的好处是差异友好:添加或删除属性或数组成员几乎不会触及任何其他行,但实际发生更改的那一行,而尾随逗号语法几乎总是触及两行以使逗号正确。

Conversely, changed lines will never have anything unrelated on them, which helps scanning commits visually immensely.相反,改变的行永远不会有任何无关的东西,这有助于在视觉上极大地扫描提交。

It probably won't improve your runtime or anything like this, but you have a vantage using the trailing comma related to the version control.它可能不会改善您的运行时或类似的东西,但您可以使用与版本控制相关的尾随逗号。

If you do not use it, git will detected that one line was modified and another one was added.如果不使用它,git 会检测到修改了一行并添加了另一行。 But if you use it, git will detected that only one line was added:但是如果你使用它,git会检测到只添加了一行:

With using trailing comma:使用尾随逗号:

在此处输入图片说明

Without using trailing comma:使用尾随逗号:

在此处输入图片说明

First of all, I think trailing commas are ugly so I only use them when necessary (python tuples with just a single element) - and leading commas are even uglier.首先,我认为尾随逗号很难看,所以我只在必要时使用它们(只有一个元素的 python 元组) - 而前导逗号甚至更丑。

Besides that, there is a reason to never use them: You don't have to take care if the code you are writing is for node.js or the client which might very well be an IE that does not like them.除此之外,还有一个永远不要使用它们的理由:您不必担心您正在编写的代码是用于 node.js 还是客户端,而客户端很可能是不喜欢它们的 IE。

There is no technical reason for omitting them in node.js.在 node.js 中省略它们没有技术原因。

I am a big fan of alpha-sorted keys in an object.我非常喜欢对象中的 alpha 排序键。 There are a number of advantages to this.这有许多优点。 The primary one being the visual comparison of two similar objects.第一个是两个相似对象的视觉比较。 All the modern IDEs that provide debugging support for JavaScript (that I've seen) alpha-sort the properties.所有为 JavaScript 提供调试支持的现代 IDE(我见过的)都对属性进行 alpha 排序。 So when you're comparing an object at a breakpoint with your code it just makes it easier.因此,当您将断点处的对象与您的代码进行比较时,它只会更容易。

Having a trailing comma on the last pair in an object makes it easy to sort the pairs in the object: Select all lines and hit the sort key in your editor.在对象中的最后一对末尾添加逗号可以很容易地对对象中的对进行排序:选择所有行并在编辑器中点击排序键。 No further adding/removing of commas is needed if the last pair is no longer the last pair.如果最后一对不再是最后一对,则不需要进一步添加/删除逗号。

I agree with all the other comments that (at first) it makes your code "unclean" or uglier.我同意所有其他评论(起初)它使您的代码“不干净”或更丑陋。 I found that after moving to trailing commas I quickly became used to it and after a number of weeks I started to prefer to see it that way.我发现在使用尾随逗号后,我很快就习惯了它,几周后我开始更喜欢这样看待它。

As humans we are most comfortable with what we are used to.作为人类,我们对我们习惯的东西感到最舒服。 Given that most of the code that we see does not have a trailing comma that is what we like the most.鉴于我们看到的大多数代码都没有我们最喜欢的尾随逗号。 After a few weeks with trailing commas you might find that you prefer that.使用尾随逗号几周后,您可能会发现您更喜欢这样。 And you might find it more practical.你可能会发现它更实用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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