简体   繁体   中英

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

  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)

The MDN states that the keyword interface is reserved for future use and may not be used for property/function/variable names.

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. 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.

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 5 lifted this restriction (they are still disallowed as variable and function names). However, the compiler uses ecmascript 3 mode by default.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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