-2

I am learning to use google closure compiler for javascript but have strange compile error

I get this message:

ctest2.js:31: ERROR - [JSC_PARSE_ERROR] Parse error. '(' expected
31|  testArray = [];
                 ^

1 error(s), 0 warning(s)

If I comment that line out the compile works.

here's the whole class:

class TestA
{

 testArray = []; // Comment this out to get rid of error

 constructor()
{

this.testArray.push( { name : "joe", age : 70 } );
this.testArray.push( { name : "mike", age : 50 } );
this.testArray.push( { name : "bob", age : 33 } );

}

testLoop()
{

for(const test of this.testArray)
{
 console.log(" >>> " + test.name + " " + test.age);
}

}

}

Why isn't it compiling properly?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Coder547
  • 119
  • 9
  • 2
    Because it's not valid JavaScript to put assignments in class bodies. Class fields are still an [experimental proposal](https://github.com/tc39/proposal-class-fields) only, and you'll need to enable support for this syntax explicitly in the options (if gcc supports them at all). – Bergi Jul 26 '20 at 16:33
  • Ok thx for letting me know. I have used this code in google chrome without problems though – Coder547 Jul 26 '20 at 16:36
  • 1
    Yes, Chrome's js engine already implements the proposal – Bergi Jul 26 '20 at 16:40

1 Answers1

0

Reason is because that kind of variable declaring inside class is not yet supported in the google-closure-compiler. But it's supported for example in google chrome

Coder547
  • 119
  • 9