So I've been reading up on proposed JavaScript features, and one I'm concerned about is trailing comma support in object literals and arrays.
For parameters, trailing commas don't relate here, so let's ignore that. I understand the Version Control benefits, but I'm worried how it will react to JSON.
const arr = [
'red',
'green',
'blue',
];
This would become valid.
But what will happened when you return JSON syntax? JSON is supported by RFC, so I doubt JSON will ever support trailing commas. Maybe one day..
But how will JavaScript handle returning something like:
const jsonReturn = [{
"derp":1
}, {
"foo":"bar"
}, {
"slide":true,
},];
Will the trailing comma be removed internally if the header content type is JSON or will trailing commas break everything?
You won't run into any problems, because JSON and JS source have nothing to do with each other.
JSON does not (and for the sake of example, will not) support trailing commas. The current JSON spec clearly shows that commas may only occur between values within an object or array.
If JS does introduce support for trailing commas, the source representation of the object and the version that is serialized are largely unrelated. Most browsers today will accept a trailing comma, but all commas are discarded in the actual object (dict/hash or struct) representation:
> var foo = {bar: 1, baz: 2,};
< undefined
> foo
< Object {bar: 1, baz: 2}
Even today, serializing an object with a trailing comma works just fine:
> JSON.stringify({bar: 1, baz: 2,})
< "{"bar":1,"baz":2}"
The commas are for parsing source only and do not exist in the runtime's object representation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With