Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 871
Govind Malviya Avatar asked Dec 27 '22 21:12

Govind Malviya


2 Answers

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'
};
like image 90
Niko Avatar answered Jan 09 '23 03:01

Niko


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.

like image 24
John Avatar answered Jan 09 '23 02:01

John