]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-this-before-super.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / no-this-before-super.md
1 ---
2 title: no-this-before-super
3 rule_type: problem
4 ---
5
6
7
8 In the constructor of derived classes, if `this`/`super` are used before `super()` calls, it raises a reference error.
9
10 This rule checks `this`/`super` keywords in constructors, then reports those that are before `super()`.
11
12 ## Rule Details
13
14 This rule is aimed to flag `this`/`super` keywords before `super()` callings.
15
16 ## Examples
17
18 Examples of **incorrect** code for this rule:
19
20 ::: incorrect
21
22 ```js
23 /*eslint no-this-before-super: "error"*/
24 /*eslint-env es6*/
25
26 class A extends B {
27 constructor() {
28 this.a = 0;
29 super();
30 }
31 }
32
33 class A extends B {
34 constructor() {
35 this.foo();
36 super();
37 }
38 }
39
40 class A extends B {
41 constructor() {
42 super.foo();
43 super();
44 }
45 }
46
47 class A extends B {
48 constructor() {
49 super(this.foo());
50 }
51 }
52 ```
53
54 :::
55
56 Examples of **correct** code for this rule:
57
58 ::: correct
59
60 ```js
61 /*eslint no-this-before-super: "error"*/
62 /*eslint-env es6*/
63
64 class A {
65 constructor() {
66 this.a = 0; // OK, this class doesn't have an `extends` clause.
67 }
68 }
69
70 class A extends B {
71 constructor() {
72 super();
73 this.a = 0; // OK, this is after `super()`.
74 }
75 }
76
77 class A extends B {
78 foo() {
79 this.a = 0; // OK. this is not in a constructor.
80 }
81 }
82 ```
83
84 :::
85
86 ## When Not To Use It
87
88 If you don't want to be notified about using `this`/`super` before `super()` in constructors, you can safely disable this rule.