首页 > 解决方案 > Javascript 闭包编译器

问题描述

我正在学习为 javascript 使用谷歌闭包编译器,但有奇怪的编译错误

我收到这条消息:

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

1 error(s), 0 warning(s)

如果我将该行注释掉,则编译工作。

这是整个班级:

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

}

}

为什么编译不正确?

标签: javascriptgoogle-closure-compiler

解决方案


原因是因为 google-closure-compiler 尚不支持在类中声明的那种变量。但例如在 google chrome 中支持它


推荐阅读