ES proposal: Trailing commas in function parameter lists and calls


The following ECMAScript proposal is at stage 3: “Trailing commas in function parameter lists and calls” by Jeff Morrison. This blog post explains it.



Trailing commas in object literals and Array literals

Trailing commas are ignored in object literals:



let obj = {
first: 'Jane',
last: 'Doe',
};

And they are also ignored in Array literals:



let arr = [
'red',
'green',
'blue',
];
console.log(arr.length); // 3

Why is that useful? There are two benefits.


First, rearranging items is simpler, because you don’t have to add and remove commas if the last item changes its position.


Second, it helps version control systems with tracking what actually changed. For example, going from:



[
'foo'
]

to:



[
'foo',
'bar'
]

leads to both the line with 'foo' and the line with 'bar' being marked as changed, even though the only real change is the latter line being added.


Proposal: allow trailing commas in parameter definitions and function calls

Given the benefits of optional and ignored trailing commas, the proposed feature is to bring them to parameter definitions and function calls.


For example, the following function declaration causes a SyntaxError in ECMAScript 6, but would be legal with the proposal:



function foo(
param1,
param2,
) {}

Similarly, the proposal would make this invocation of foo() syntactically legal:



foo(
'abc',
'def',
);


Comments

Popular posts from this blog

Steve Lopez and the Importance of Newspapers

What is the true nature of lions and hyenas?

A Treasure Hunt Without The Treasure