#Why does throw Error not need new operator

1 messages · Page 1 of 1 (latest)

jolly bladeBOT
#
luziferxyz#3613

Preview:```ts
export class CustomError extends Error {
constructor(message: string) {
super(message)
this.name = "CustomError"
}
}

const foo = "throwCustomError"

if (foo === "throwCustomError") {
throw CustomError("Custom Error throws")
} else {
throw Error("Default Error class used")
...```

buoyant nova
#

@green cipher It's just a quirk of how the Error function is written - in JS a function can support both being called as a function as a function and as a constructor, and a few older JS APIs handle both cases.

unkempt holly
#

generally constructing with new seems to be preferred in modern js

green cipher
#

!resolved