Eslint
规则 | Rules

no-dupe-class-members

禁止在级别成员中使用重复名称(no-dupe-class-members)

配置文件中的"extends": "eslint:recommended"属性启用此规则。

如果在类成员中有相同名称的声明,则最后的声明会无声地覆盖其他声明。它可能会导致意外的行为。

/*eslint-env es6*/ class Foo { bar() { console.log("hello" } bar() { console.log("goodbye" } } var foo = new Foo( foo.bar( // goodbye

规则细节

此规则旨在标记在级别成员中使用重复名称。

示例

此规则的错误代码示例:

/*eslint no-dupe-class-members: "error"*/ /*eslint-env es6*/ class Foo { bar() { } bar() { } } class Foo { bar() { } get bar() { } } class Foo { static bar() { } static bar() { } }

此规则的正确代码示例:

/*eslint no-dupe-class-members: "error"*/ /*eslint-env es6*/ class Foo { bar() { } qux() { } } class Foo { get bar() { } set bar(value) { } } class Foo { static bar() { } bar() { } }

何时不使用它

此规则不应用于 ES3/5环境。

在 ES2015(ES6)或更高版本中,如果您不希望在类成员中收到重复名称的通知,则可以安全地禁用此规则。

版本

该规则在 ESLint 1.2.0中引入。

资源