为什么 javascript 让你在函数参数的末尾添加逗号?

Why does javascript let you add comma in the end of function parameters?

我在 javascript 函数声明中遇到了似乎已关闭的更改。您可以创建这样的函数:

let a = function (b,) {
    console.log(b);
}

我发现函数参数中的尾随逗号是允许的,因为 git 之间存在差异:

let a = function (
    b,
) {
    console.log(b);
}

let a = function (
    b,
    c,
) {
    console.log(b);
}

是 git 差异的真正原因,因为它有效我只相信 ECMAScript-2017。

ECMAScript 2017 allows trailing commas in function parameter lists. 来自 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Trailing_commas.
规范允许。
我不认为此功能的唯一原因是帮助控制系统(如 git)进行跟踪,即使我认为这是一个很好的...
另一个原因是重新排列项目更简单,因为如果最后一项改变其位置,您不必添加和删除逗号。据我所知,这是主要原因......在引入之前,我确实经常问自己为什么JS对我们这些可怜的开发人员如此严格......:-)

Is the git diffs really the reason for that as it works I believe only in the ECMAScript-2017.

基本上,答案是肯定的。引用原文proposal(粗体字是我的)

In some codebases/style guides there are scenarios that arise where function calls and definitions are split across multiple lines in the style of:

 1: function clownPuppiesEverywhere(
 2:   param1,
 3:   param2
 4: ) { /* ... */ }
 5: 
 6: clownPuppiesEverywhere(
 7:   'foo',
 8:   'bar'
 9: );

In these cases, when some other code contributor comes along and adds another parameter to one of these parameter lists, they must make two line updates:

 1: function clownPuppiesEverywhere(
 2:   param1,
 3:   param2, // updated to add a comma
 4:   param3  // updated to add new parameter
 5: ) { /* ... */ }
 6: 
 7: clownPuppiesEverywhere(
 8:   'foo',
 9:   'bar', // updated to add a comma
10:   'baz'  // updated to add new parameter
11: );

In the process of doing this change on code managed by a version control system (git, subversion, mercurial, etc), the blame/annotation code history information for lines 3 and 9 get updated to point at the person who added the comma (rather than the person who originally added the parameter).

To help mitigate this problem, some other languages (Python, D, Hack, ...probably others...) have added grammar support to allow a trailing comma in these parameter lists. This allows code contributors to always end a parameter addition with a trailing comma in one of these per-line parameter lists and never have to worry about the code attribution problem again