var err1 = Error(\'message\');
var err2 = new Error(\'message\');
What\'s the difference? Looking at them in the chrome console, they look identica
Error
does act like a factory, like some other native constructors: Array
, Object
, etc. all check something like if (!(this instanceof Array)) { return new Array(arguments); }
. (But note that String(x)
and new String(x)
are very different, and likewise for Number
and Boolean
.)
That said, in case of an error, it's not even required to throw an 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'};
Both are fine; this is explicitly stated in the specification:
... Thus the function call
Error(…)
is equivalent to the object creation expressionnew Error(…)
with the same arguments.