首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

no-dupe-class-members

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

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

代码语言:javascript
复制
/*eslint-env es6*/

class Foo {
  bar() { console.log("hello"); }
  bar() { console.log("goodbye"); }
}

var foo = new Foo();
foo.bar(); // goodbye

规则细节

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

示例

此规则的错误代码示例:

代码语言:javascript
复制
/*eslint no-dupe-class-members: "error"*/
/*eslint-env es6*/

class Foo {
  bar() { }
  bar() { }
}

class Foo {
  bar() { }
  get bar() { }
}

class Foo {
  static bar() { }
  static bar() { }
}

此规则的正确代码示例:

代码语言:javascript
复制
/*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中引入。

资源

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com