Skip to content

Commit d8ce4da

Browse files
authored
Added section about this in class methods
1 parent 45759fc commit d8ce4da

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
- [9.4](#constructors--tostring) toString methods
141141
- [9.5](#constructors--no-useless) No empty constructors
142142
- [9.6](#classes--no-duplicate-members) No duplicate members
143+
- [9.7](#classes--methods-use-this) `this` in Class-Methods
143144

144145
</details>
145146

@@ -1717,6 +1718,39 @@
17171718
bar(){ return 2; }
17181719
}
17191720
```
1721+
1722+
<a name="classes--methods-use-this"></a>
1723+
- [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)
1724+
1725+
```javascript
1726+
// bad
1727+
class Foo {
1728+
bar(){
1729+
console.log("bar");
1730+
}
1731+
}
1732+
1733+
// good - this is used
1734+
class Foo {
1735+
bar(){
1736+
console.log(this.bar);
1737+
}
1738+
}
1739+
1740+
// good - constructor is exempt
1741+
class Foo {
1742+
constructor(){
1743+
// ...
1744+
}
1745+
}
1746+
1747+
// good - static methods aren't expected to use this
1748+
class Foo {
1749+
static bar(){
1750+
console.log("bar");
1751+
}
1752+
}
1753+
```
17201754

17211755
**[⬆ back to top](#table-of-contents-bookmark_tabs)**
17221756

0 commit comments

Comments
 (0)