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