[guide] Document class-methods-use-this

This PR documents the currently enforced `class-methods-use-this` ESlint rule as discussed in #2129
This commit is contained in:
Igor Gassmann
2019-11-03 21:00:57 +01:00
committed by Jordan Harband
parent 64b965efe0
commit 56b75dc359

View File

@@ -1254,6 +1254,39 @@ Other Style Guides
}
```
<a name="classes--methods-use-this"></a>
- [9.7](#classes--methods-use-this) Class methods should use `this` or be made into a static method unless an external library or framework requires to use specific non-static methods. Being an instance method should indicate that it behaves differently based on properties of the receiver. eslint: [`class-methods-use-this`](https://eslint.org/docs/rules/class-methods-use-this)
```javascript
// bad
class Foo {
bar() {
console.log('bar');
}
}
// good - this i used
class Foo {
bar() {
console.log(this.bar);
}
}
// good - constructor is exempt
class Foo {
constructor() {
// ...
}
}
// good - static methods aren't expected to use this
class Foo {
static bar() {
console.log('bar');
}
}
```
**[⬆ back to top](#table-of-contents)**
## Modules