解析错误:意外的字符'#'在宣布JavaScript课程的Priavate成员时

发布于 2025-01-30 07:22:27 字数 780 浏览 2 评论 0 原文

当使用“#”在JavaScript类中声明私人成员时,ESLINT会引发错误解析错误:意外字符'#'

例如。

class Test{
  #priavteMember; //Parsing error: Unexpected character '#'
}

ESLINT 配置:.eslintrc.json

{
    "env": {
        "browser": true,
        "es2021": true,
        "node": true
    },
    "extends": [
        "airbnb-base"
    ],
    "rules": {
        "no-use-before-define": "off",
        "no-param-reassign": "off",
        "no-plusplus": "off",
        "no-nested-ternary": "off",
        "lines-between-class-members": "off"
    },
    "globals": {
        "root": "readonly",
        "app": "readonly",
        "db": "readonly"
    }
}

When using '#' to declare a private member in a javascript class, eslint throws an error Parsing error: Unexpected character '#'.

For eg.

class Test{
  #priavteMember; //Parsing error: Unexpected character '#'
}

eslint configuration: .eslintrc.json

{
    "env": {
        "browser": true,
        "es2021": true,
        "node": true
    },
    "extends": [
        "airbnb-base"
    ],
    "rules": {
        "no-use-before-define": "off",
        "no-param-reassign": "off",
        "no-plusplus": "off",
        "no-nested-ternary": "off",
        "lines-between-class-members": "off"
    },
    "globals": {
        "root": "readonly",
        "app": "readonly",
        "db": "readonly"
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谎言月老 2025-02-06 07:22:27

将其添加到.eslintrc.json

{
  ...
  "parserOptions": {
      "ecmaVersion": 13,
  },
  ...
}

私有成员指定符中,以 ecma 版本13。

Add this to .eslintrc.json

{
  ...
  "parserOptions": {
      "ecmaVersion": 13,
  },
  ...
}

Private member specifier was introduced in ECMA version 13.

月野兔 2025-02-06 07:22:27

Use the es2022 environment, which will automatically set the ecmaVersion parser option to 13, as indicated in the documentation:

    "env": {
        "browser": true,
        "es2022": true,
        "node": true
    },
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文