在 JS 中声明多个私有类方法时,我收到 ESLINT 警告,为什么?

Gug*_*der 8 javascript ecmascript-6 eslint es6-class

如果我在类中只声明一个私有方法,则 ESLINT 编译时不会发出警告,但是...\nESLINT 会为我在同一类中声明的每个新私有方法发出一个额外警告。

\n

例如,ESLINT 对于如下声明的类发出 2 个警告:

\n
class Clazz {\n  #methodOne() { /*...*/ }\n  #methodTwo() { /*...*/ }\n  #methodThree() { /*...*/ }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

ESLINT 输出:

\n
WARNING  Compiled with 1 warning                                                                                                                                   10:14:26 PM\n\nModule Warning (from ./node_modules/eslint-loader/index.js):\n\n/Users/***/Clazz.js\n  3:3  error  Duplicate name \'\'  no-dupe-class-members\n  4:3  error  Duplicate name \'\'  no-dupe-class-members\n\n\xe2\x9c\x96 2 problems (2 errors, 0 warnings)\n
Run Code Online (Sandbox Code Playgroud)\n

我想知道我是否做错了什么,或者我应该忽略这些警告。

\n
\n

包.json:

\n
WARNING  Compiled with 1 warning                                                                                                                                   10:14:26 PM\n\nModule Warning (from ./node_modules/eslint-loader/index.js):\n\n/Users/***/Clazz.js\n  3:3  error  Duplicate name \'\'  no-dupe-class-members\n  4:3  error  Duplicate name \'\'  no-dupe-class-members\n\n\xe2\x9c\x96 2 problems (2 errors, 0 warnings)\n
Run Code Online (Sandbox Code Playgroud)\n

cla*_*pas 3

我遇到了同样的问题并找到了解决方法。像这样定义你的私有方法:

class Clazz {
  #methodOne = function() { /*...*/ }
  #methodTwo = function() { /*...*/ }
  #methodThree = function() { /*...*/ }
}
Run Code Online (Sandbox Code Playgroud)

并且 linter 警告/错误将会消失。