]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-constructor-return.md
import 8.23.1 source
[pve-eslint.git] / eslint / docs / src / rules / no-constructor-return.md
1 ---
2 title: no-constructor-return
3 layout: doc
4 rule_type: problem
5 ---
6
7
8 In JavaScript, returning a value in the constructor of a class may be a mistake. Forbidding this pattern prevents mistakes resulting from unfamiliarity with the language or a copy-paste error.
9
10 ## Rule Details
11
12 This rule disallows return statements in the constructor of a class. Note that returning nothing with flow control is allowed.
13
14 Examples of **incorrect** code for this rule:
15
16 ::: incorrect
17
18 ```js
19 /*eslint no-constructor-return: "error"*/
20
21 class A {
22 constructor(a) {
23 this.a = a;
24 return a;
25 }
26 }
27
28 class B {
29 constructor(f) {
30 if (!f) {
31 return 'falsy';
32 }
33 }
34 }
35 ```
36
37 :::
38
39 Examples of **correct** code for this rule:
40
41 ::: correct
42
43 ```js
44 /*eslint no-constructor-return: "error"*/
45
46 class C {
47 constructor(c) {
48 this.c = c;
49 }
50 }
51
52 class D {
53 constructor(f) {
54 if (!f) {
55 return; // Flow control.
56 }
57
58 f();
59 }
60 }
61 ```
62
63 :::