5

I'm trying to follow the JS code style defined by Airbnb.

The rule on trailing commas for function call arguments states:

7.15 Functions with multiline signatures, or invocations, should be indented just like every other multiline list in this guide: with each item on a line by itself, with a trailing comma on the last item.

But when I do the following:

/* THREE.js constructor for PerspectiveCamera */
const camera = new THREE.PerspectiveCamera(
    75,
    window.innerWidth / window.innerHeight,
    0.1,
    1000,
);

Google Chrome complains with the following error:

app.js:11 Uncaught SyntaxError: Unexpected token )

When I remove the trailing comma everything works fine. This code works fine in Firefox and I am fairly sure it worked a week ago from today (11.04.2017) in Chrome as well - because I haven't changed my code since than and I was presenting the app I'm working on to my colleague.

Note that trailing comma in arrays still works fine:

testArray = [
    'one',
    'two',
    'three',
];

Can someone explain this behavior or point me to where I can look for more information?

Using Google Chrome (Version 57.0.2987.133 (64-bit)) on Ubuntu 16.04.

miken32
  • 42,008
  • 16
  • 111
  • 154
bmakan
  • 332
  • 3
  • 11
  • It is legal syntax and is often used in large code bases where miltuple people might be adding parameters to functions etc. But it's also a setting in most LINTers, since it does have effects on the length property of functions and arrays, so it can create bugs. Also, IE doesn't like them. Also ahve a look at http://stackoverflow.com/questions/7246618/are-trailing-commas-in-arrays-and-objects-part-of-the-spec – Shilly Apr 11 '17 at 07:34
  • @Shilly - Chrome and FF don't like trailing commas in function arguments either. – nnnnnn Apr 11 '17 at 07:41
  • 2
    Depends on the version and settings used. Imho trailing commas should be avoided. Hence I don't like the airbnb style. But that's personaly opinion. – Shilly Apr 11 '17 at 07:43
  • Relevant specification references: [_Arguments_](//tc39.es/ecma262/#prod-Arguments), [ArgumentListEvaluation](//tc39.es/ecma262/#sec-runtime-semantics-argumentlistevaluation). MDN documentation: [Trailing commas](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Trailing_commas#function_calls), also contains a [compatibility table](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Trailing_commas#browser_compatibility). Original proposal: [Proposal to allow trailing commas in function parameter lists](//github.com/tc39/proposal-trailing-function-commas). – Sebastian Simon Dec 01 '22 at 06:36

1 Answers1

4

My team just ran into this issue with a user who has Chrome 55.0.2883.87. This version of Chrome also reports unexpected token at ')' as reported above.

The trailing commas DO seem to be TOLERATED by Chrome 60.0.3112.113. No error.

So we can deduce that Google is moving towards support for the trailing comma.

Pang
  • 9,564
  • 146
  • 81
  • 122
Paul Cuddihy
  • 477
  • 6
  • 12