5

我使用html5boilerplate構建腳本和涅槃的腳本(使用谷歌關閉編譯)IE瀏覽器,使用Closure Compiler和尾隨逗號

當我得到這個錯誤

-js.all.minify: 
    [echo] Minifying scripts 
    [copy] Copying 3 files to /Users/Username/Desktop/Web/intermediate/js 
    [apply] /Users/Juan/Desktop/Web/js/plugins.js:117: ERROR - Parse error. Internet Explorer has a non-standard intepretation of trailing commas. Arrays will have the wrong length and objects will not parse at all. 
    [apply]     }, { duration: 727 }) 
    [apply] 

      ^

但代碼如果運行未編譯,是否可以在IE 8中運行。

這是代碼

anim1.animate({ 
        'left': '+=32px', 
        'filter': 'alpha(opacity=100)', 
        '-moz-opacity': '1', 
        '-khtml-opacity': '1', 
        'opacity': '1', 
       }, { duration: 727 }) 

我怎樣才能使此代碼通Compulsure編譯器?

感謝

回答

10

從對象文本中刪除多餘的最後一個逗號:

anim1.animate({ 
    'left': '+=32px', 
    'filter': 'alpha(opacity=100)', 
    '-moz-opacity': '1', 
    '-khtml-opacity': '1', 
    'opacity': '1'  // <-- No comma here. 
}, { duration: 727 }); // <-- I'd also suggest a semicolon there. 

由於Closure編譯器說,這樣的尾隨逗號文字不能被某些瀏覽器解析。

+0

我在這段代碼上花費太多,我無法看到它在我的眼中。我仍然覺得IE 7,8,9中的代碼工作正常,但編譯器認爲它存在某種致命錯誤。謝謝Frédéric。 – Juan 2012-08-06 11:55:09

+1

這是一篇解釋原因的文章:http://www.enterprisedojo.com/2010/12/19/beware-the-trailing-comma-of-death/。您可以使用'--jscomp_warning internetExplorerChecks'標誌將此錯誤轉換爲警告。 – 2012-08-06 15:43:21

+0

如果JSHint和JSLint告訴我這件事真的很高興,因爲我整天都在浪費這個...... SMDH謝謝 – 2013-12-16 18:44:27

4

或啓用EcmaScript 5模式。 Ecmascript 5標準化了尾部逗號行爲,但IE8並未完全支持ES5(IE9沒有缺少嚴格模式)。