应用程序中有一个 App
类:
class App {
static cart;
static init() {
const shop = new Shop();
shop.render();
this.cart = shop.cart;
}
}
我使用 VSCode
作为 IDE,使用 jshint
作为代码质量工具。
将 static cart;
声明为 App
类字段会导致问题面板
中出现下一条错误消息:
(property) App.cart: any
Class properties must be methods. Expected '(' but instead saw ';'. (E054)jshint(E054)
我尝试用谷歌搜索这个问题,但失败了。
你能告诉我我做错了什么吗?
我是 JS 新手,所以我的代码中可能存在语法错误?
最佳答案
我不清楚你的代码应该做什么(例如,我不知道 Shop
是什么/做什么),但我可以告诉你类字段定义 are allowed并且不再是实验性的 as of ES2022 spec .
我认为这是当前版本的 jshint
(撰写本文时为 v2.13.4)中的错误/限制,因为它尚未识别 class fields 。请参阅https://github.com/jshint/jshint/issues/3139 ,跟踪问题。
对于该项目来说不幸的是,JSHint 近年来很难跟上其他工具的步伐。如上所述here一位维护者认为,这至少部分是由于其聪明、幽默但令人惊讶的有问题的许可协议(protocol)。
另请参阅:
关于javascript - 为什么当我声明类字段时 jshint 会抛出错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65641937/