Constructors should not return values.
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.
Do not return anything from the constructor.
class TextMessage {
constructor(msg) {
this.text = msg;
return msg; // Noncompliant
}
}
class TextMsg1 {
constructor(msg) {
this.text = msg;
}
}
class TextMsg2 {
constructor(msg) {
if (!msg) {
return; // ok to return nothing for flow control
}
this.text = msg;
}
}