Skip to content

noConstructorReturn

诊断类别:lint/correctness/noConstructorReturn

¥Diagnostic Category: lint/correctness/noConstructorReturn

自从:v1.0.0

¥Since: v1.0.0

来源:

¥Sources:

禁止从 constructor 返回值。

¥Disallow returning a value from a constructor.

从类的 constructor 返回值可能是一个错误。禁止此模式可防止因不熟悉 JavaScript 或复制粘贴错误而导致的错误。

¥Returning a value from a constructor of a class is a possible error. Forbidding this pattern prevents errors resulting from unfamiliarity with JavaScript or a copy-paste error.

仅允许返回没有值,因为它是控制流语句。

¥Only returning without a value is allowed, as it’s a control flow statement.

¥Examples

¥Invalid

class A {
constructor() {
return 0;
}
}
code-block.js:3:9 lint/correctness/noConstructorReturn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The constructor should not return a value.

1 │ class A {
2 │ constructor() {
> 3 │ return 0;
^^^^^^^^^
4 │ }
5 │ }

The constructor is here:

1 │ class A {
> 2 │ constructor() {
^^^^^^^^^^^^^^^
> 3 │ return 0;
> 4 │ }
^
5 │ }
6 │

Returning a value from a constructor is ignored.

¥Valid

class A {
constructor() {}
}
class B {
constructor(x) {
return;
}
}

¥Related links