Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

issue regarding trailing commas in JavaScript [duplicate]

Possible Duplicate:
Internet Explorer, Closure Compiler and Trailing Commas

I've tried compressing my javascript code using the Closure Compiler and the compilation of the code generated these two errors:

JSC_TRAILING_COMMA: Parse error. IE8 (and below) will parse trailing commas in array and object literals incorrectly. If you are targeting newer versions of JS, set the appropriate language_in option. at line 379 character 0 fontFamily : jqTextareaDiv.css("font-family").replace(/["']{1}/gi,""),

JSC_TRAILING_COMMA: Parse error. IE8 (and below) will parse trailing commas in array and object literals incorrectly. If you are targeting newer versions of JS, set the appropriate language_in option. at line 932 character 0 fontFamily : jqDiv.css("font-family"),

These two errros seem to refer to this code:

var jqTextareaDiv = obj.target.parent().parent(),
                            style = {       // the current, relevant style rules for the DIV nesting the textarea
                                fontFamily     : jqTextareaDiv.css("font-family").replace(/["']{1}/gi,""),
                                fontSize       : jqTextareaDiv.css("font-size"),
                                fontStyle      : jqTextareaDiv.css("font-style"),
                                fontWeight     : jqTextareaDiv.css("font-weight"),
                                textDecoration : jqTextareaDiv.css("text-decoration"),
                                textAlign      : jqTextareaDiv.css("text-align"),
                                color          : jqTextareaDiv.css("color"),
                            },
                            jqToolbox = $('#text-edit-toolbox'),
                            jqIndicators = {
                                fontFamily                : $('#font-family-indicator'),
                                fontSize                  : $('#font-size-indicator'),
                                fontStyle                 : $('#font-format-indicators .font-style'),
                                fontWeight                : $('#font-format-indicators .font-weight'),
                                textDecorationUnderline   : $('#font-format-indicators .underline'),
                                textDecorationLineThrough : $('#font-format-indicators .line-through'),
                                textAlignLeft             : $('#text-alignment-indicators .align-left'),
                                textAlignCenter           : $('#text-alignment-indicators .align-center'),
                                textAlignRight            : $('#text-alignment-indicators .align-right'),
                                textAlignJustify          : $('#text-alignment-indicators .align-justify')
                            };

Exactly which is the trailing comma in this case and how can I remove it without breaking the code?

like image 462
Andrei Oniga Avatar asked Aug 22 '12 15:08

Andrei Oniga


People also ask

Is trailing commas allowed in JavaScript?

JavaScript has allowed trailing commas in array literals since the beginning, and later added them to object literals, and more recently, to function parameters and to named imports and named exports. JSON, however, disallows trailing commas.

Should we use trailing commas?

In general, you should make use of trailing commas when you frequently copy/paste properties or add new items to the end of a list. You can also take advantage of them to produce cleaner diff outputs.

Can trailing commas be used in objects and arrays JSON?

They are optional. There is no reason at all commas need to be present to parse a JSON-like document.

Do you need commas in JavaScript?

JavaScript uses a comma ( , ) to represent the comma operator. A comma operator takes two expressions, evaluates them from left to right, and returns the value of the right expression. In this example, the 10, 10+20 returns the value of the right expression, which is 10+20. Therefore, the result value is 30.


2 Answers

A trailing comma is a comma that follows the final element in an array or object literal. So like this:

['a', 'b', 'c',] // with trailing comma
['a', 'b', 'c']  // without trailing comma

In this case, the trailing comma follows the last element in your object literal:

color          : jqTextareaDiv.css("color"),

If you remove it, the expected behaviour will occur. With it there, IE<9 won't like it.

like image 92
lonesomeday Avatar answered Oct 05 '22 12:10

lonesomeday


This is the trailing comma:

color          : jqTextareaDiv.css("color"), <<--
like image 34
Aaron Digulla Avatar answered Oct 05 '22 11:10

Aaron Digulla