javascript - 关于 JavaScript 中尾随逗号的问题

标签 javascript google-closure-compiler

<分区>

Possible Duplicate:
Internet Explorer, Closure Compiler and Trailing Commas

我尝试使用 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"),

这两个错误好像是指这段代码:

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')
                            };

在这种情况下,尾随的逗号到底是什么?如何在不破坏代码的情况下将其删除?

最佳答案

尾随逗号是跟在数组或对象文字中最后一个元素之后的逗号。所以像这样:

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

在这种情况下,尾随逗号跟在对象文字中的最后一个元素之后:

color          : jqTextareaDiv.css("color"),

如果删除它,就会发生预期的行为。有了它,IE<9 不会喜欢它。

关于javascript - 关于 JavaScript 中尾随逗号的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12076373/

相关文章:

javascript - 您如何从从 index.html 导入的 Javascript 脚本访问全局变量和函数并在组件中使用它们?

javascript - 如何使用 TypeScript 和 Angular 2 向 ng2-ckeditor 添加插件?

javascript - Redux 异步中间件操作没有 api 调用所需的现有状态

javascript - 将 3rd 方库与 requirejs 一起使用

javascript - mongoDB - 游标仅使用 node.js 驱动程序更新 1 个文档

javascript - 如何获取包含已知文本的链接

node.js - Google Closure 编译器移动了?它给出了 302 错误

javascript - 具有静态字段的函数的闭包编译器外部

javascript - Google Closure 在 Chrome 扩展编译期间需要 RequestFilter 和 BlockingResponse 类型。如何创建这样的类型?

javascript - Closure Compiler 并不总是内联枚举。为什么?