I do not understand why Object.setPrototypeOf(this, DatabaseConnectionError.prototype); is needed in the source code below.
export class DatabaseConnectionError extends Error {
reason = "Error connecting to database";
constructor() {
super("Error connecting to database");
Object.setPrototypeOf(this, DatabaseConnectionError.prototype);
}
}
Instantiating a class results in the instance inheriting from the class (having the class's prototype in its prototype-chain) by default, so there should not be a need to set the prototype to the class's explicitely.
While the following is not the case, here is a similar scenario: Setting the prototype after construction of the class allows the instance to e.g. inherit methods from a different class instead.
Note: This does however obfuscate from which class the object is constructed from; see third log above.
Regardless, I'd argue you should prefer a sensible prototype-chain over whatever above is.