3

I have encountered a change in the javascript function declaration that seems to be off. You can make a function like this:

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

I have found that the trailing comma in function parameters is allowed because of git differences between:

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

and

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

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

D. Mydło
  • 51
  • 5

2 Answers2

3

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

Basically, the answer is yes. Quoting original proposal (bold text is mine)

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

Yury Tarabanko
  • 44,270
  • 9
  • 84
  • 98
1

ECMAScript 2017 allows trailing commas in function parameter lists. from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Trailing_commas.
It is allowed by Specification.
I don't think the only reason for this feature was to help control systems (like git) with tracking, even id this is a good one...
Another reason is that rearranging items is simpler, because you don’t have to add and remove commas if the last item changes its position. It is the main reason, according to me... Before it was introduced, I did always ask myself why JS was so strict with us poor developers... :-)

MarcoS
  • 17,323
  • 24
  • 96
  • 174
  • I think the OP's part about _git diffs_ implies/means that by adding a trailing comma it helps a version control system (i.e. git) to track what actually changed. See example given [here](http://exploringjs.com/es2016-es2017/ch_trailing-comma-parameters.html#_trailing-commas-in-object-literals-and-array-literals) starting at the paragraph reading: _"Second, it helps version control systems..."_ – RobC Feb 21 '19 at 11:35