Sam Sam - 2 months ago 22
Javascript Question

Javascript Closure Compiler Error Trailing Comma?

When minifying JavaScript in the handy tool by Google called Closure Compiler it gives the following error (and doesn't minify a thing):


Number of errors: 1 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 389 character 1 in all.js


theme : 'default', // The theme of the slider (default, theme1, theme2, t...


Where is the fault and what needs to be changed to fix the error?



$(window).load(function(){
jQuery('#slider1').autoSlider({
theme : 'default', // The theme of the slider (default, theme1, theme2, theme3)
width : '960', // The width of the slider, 100% for auto full width, set to 0 it will take the width of the largest image
autoHidePlayBtn : true, //

Answer

The fault (for fear of simply rewording what the Closure Compiler error says) is that IE8 and below cannot parse object literals and array literals which have a trailing comma.

var obj = {
    a: 1,
    b: 2, // trailing comma is bad!
};

... vs...

var obj = {
    a: 1,
    b: 2 // no trailing comma!
};

The fix is to remove the trailing comma.

Comments