简体   繁体   English

Closure编译器与对象的问题

[英]Closure compiler issues with object

I am trying to compile my Js code in google closure compiler and I am getting error on this code 我试图在谷歌闭包编译器中编译我的Js代码,我在这段代码上收到错误

  var settings = {
                 providers: ['A', 'B', 'C'],
                 interface: 'basic16',
                 apikey: 'XXXXX-XXXXX-XXXXX-XXXXXXXXXX'
                 }

Errors are 错误是

JSC_PARSE_ERROR: Parse error. invalid property id at line 3 character 10
interface: 'basic16',
          ^
JSC_PARSE_ERROR: Parse error. syntax error at line 3 character 11
interface: 'basic16',
           ^
JSC_PARSE_ERROR: Parse error. syntax error at line 4 character 8
apikey: 'XXXXX-XXXXX-XXXXX-XXXXXXXXXX'
        ^

But this code works perfect for me in any browser (chrome, firefox, opera, safari, IE7,8,9) 但是这个代码在任何浏览器中都适用于我(chrome,firefox,opera,safari,IE7,8,9)

The MDN states that the keyword interface is reserved for future use and may not be used for property/function/variable names. MDN声明关键字interface保留供将来使用,不能用于属性/函数/变量名称。

https://developer.mozilla.org/en/JavaScript/Reference/Reserved_Words https://developer.mozilla.org/en/JavaScript/Reference/Reserved_Words

Thing is, that the MDN also states that the usage of this keyword is only restricted when in strict mode. 事实是,MDN还声明此关键字的使用仅在严格模式下受到限制。 So I'm not quite sure whether the closure compiler is doing the right thing when it complains about this even in non-strict mode. 因此,即使在非严格模式下,当我抱怨这个时,我也不太确定闭包编译器是否正在做正确的事情。 This looks more like a bug, but it's probably better to avoid using these keywords anyway. 这看起来更像是一个bug,但最好还是避免使用这些关键字。

However, a solution is to simply wrap the identifier in quotation marks: 但是,解决方案是简单地将标识符包装在引号中:

var settings = {
    providers: ['A', 'B', 'C'],
    'interface': 'basic16',
    apikey: 'XXXXX-XXXXX-XXXXX-XXXXXXXXXX'
};

Ecmascript 3 disallowed keywords and reserved keywords as property names. Ecmascript 3禁止将关键字和保留关键字作为属性名称。 Ecmascript 5 lifted this restriction (they are still disallowed as variable and function names). Ecmascript 5解除了这个限制(他们仍然不允许作为变量和函数名称)。 However, the compiler uses ecmascript 3 mode by default. 但是,编译器默认使用ecmascript 3模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM