mirror of
https://github.com/airbnb/javascript.git
synced 2026-04-25 03:00:19 -04:00
[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:
committed by
Jordan Harband
parent
64b965efe0
commit
56b75dc359
33
README.md
33
README.md
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user