关于 JavaScript 中尾随逗号的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12076373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
issue regarding trailing commas in JavaScript
提问by Andrei Oniga
Possible Duplicate:
Internet Explorer, Closure Compiler and Trailing Commas
I've tried compressing my javascript code using the Closure Compilerand the compilation of the code generated these two errors:
我尝试使用Closure Compiler压缩我的 javascript 代码,并且代码的编译产生了这两个错误:
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"),
JSC_TRAILING_COMMA:解析错误。IE8(及以下)将错误地解析数组和对象文字中的尾随逗号。如果您的目标是较新版本的 JS,请设置适当的 language_in 选项。在第 379 行字符 0 fontFamily : jqTextareaDiv.css("font-family").replace(/["']{1}/gi,""),
JSC_TRAILING_COMMA:解析错误。IE8(及以下)将错误地解析数组和对象文字中的尾随逗号。如果您的目标是较新版本的 JS,请设置适当的 language_in 选项。在第 932 行字符 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?
在这种情况下到底哪个是尾随逗号,如何在不破坏代码的情况下删除它?
回答by lonesomeday
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.
如果删除它,则会发生预期的行为。有了它,IE<9 不会喜欢它。
回答by Aaron Digulla
This is the trailing comma:
这是尾随逗号:
color : jqTextareaDiv.css("color"), <<--
回答by mamapitufo
You have a trailing comma in color : jqTextareaDiv.css("color"),
. That would be the first warning. The second warning is probably a similar definition somewhere else in your code.
您在color : jqTextareaDiv.css("color"),
. 那将是第一个警告。第二个警告可能是代码中其他地方的类似定义。