【问题标题】:Static constructor gives syntax error in NodeJS静态构造函数在 NodeJS 中给出语法错误
【发布时间】:2022-01-21 11:58:16
【问题描述】:

我已经关注这个错误很长时间了,但我无法理解它。

代码如下:

class Instance {

    static {

    }

}

module.exports = Instance;

这就是全部。课堂上堆满了东西,但我把它们都删了,因为它们无关紧要。无论有没有它们,它都会给出相同的错误。

错误:

    static {
           ^

SyntaxError: Unexpected token '{'
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:94:18)
    at Object.<anonymous> (path\to\project\index.js:1:18)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)

我过去使用过静态构造函数,并且在其他地方测试相同的代码似乎可以工作,所以我不明白这里有什么问题。请帮帮我。

解决方案:将 Node 更新到 16.13.1,现在可以使用了。 16.7.0 还不够。

【问题讨论】:

标签: javascript node.js class static


【解决方案1】:

Node 17.3.0 支持它。这是一个 ES2022 特性。这个https://node.green/#ES2022 是一个很好的链接,可以在 Nodejs 中找到 ES 功能兼容性。您必须下载 Nodejs 的当前版本,即 17.3.0。

【讨论】:

  • 这就是答案。然而,刚刚更新到 16.13.1 为我解决了这个问题。
猜你喜欢
  • 2022-06-22
  • 2013-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-31
  • 1970-01-01
  • 2011-02-24
  • 1970-01-01
相关资源
最近更新 更多