Error acts like a factory, like some other native constructors: Array , Object , etc., Everyone checks for something like if (!(this instanceof Array)) { return new Array(arguments); } if (!(this instanceof Array)) { return new Array(arguments); } if (!(this instanceof Array)) { return new Array(arguments); } if (!(this instanceof Array)) { return new Array(arguments); } (But note that String(x) and new String(x) very different, as well as for Number and Boolean .)
However, in the event of an error, it is not even necessary to throw the Error object ... throw 'Bad things happened'; will work too
You can even throw an object literal for debugging:
throw {message:"You've been a naughty boy", context: this, args: arguments, more:'More custom info here'};
Elias Van Ootegem Nov 08 2018-12-12T00: 00Z
source share