53 lines
990 B
Plaintext

Constructors should not return values.
== Why is this an issue?
JavaScript allows returning a value from a class constructor. This obscure feature is rarely used and is more likely a bug than the developer's intention.
== How to fix it
Do not return anything from the constructor.
=== Code examples
==== Noncompliant code example
[source,javascript,diff-id=1,diff-type=noncompliant]
----
class TextMessage {
constructor(msg) {
this.text = msg;
return msg; // Noncompliant
}
}
----
==== Compliant solution
[source,javascript,diff-id=1,diff-type=compliant]
----
class TextMsg1 {
constructor(msg) {
this.text = msg;
}
}
class TextMsg2 {
constructor(msg) {
if (!msg) {
return; // ok to return nothing for flow control
}
this.text = msg;
}
}
----
== Resources
=== Documentation
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor[constructor]