You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+34Lines changed: 34 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -140,6 +140,7 @@
140
140
-[9.4](#constructors--tostring) toString methods
141
141
-[9.5](#constructors--no-useless) No empty constructors
142
142
-[9.6](#classes--no-duplicate-members) No duplicate members
143
+
-[9.7](#classes--methods-use-this)`this` in Class-Methods
143
144
144
145
</details>
145
146
@@ -1717,6 +1718,39 @@
1717
1718
bar(){ return 2; }
1718
1719
}
1719
1720
```
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-staticmethods. 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
+
```
1720
1754
1721
1755
**[⬆ back to top](#table-of-contents-bookmark_tabs)**
0 commit comments