When is `new Error()` better than `Error()`?

前端 未结 1 561
野的像风
野的像风 2021-02-12 22:36

The ES5 language spec clearly states that Error(foo) does the same thing as new Error(foo).

But I notice that in the wild, the longer new

1条回答
  •  不知归路
    2021-02-12 23:01

    Is there some reason for this?

    It's simply the habit of always calling constructors with new. Consistency rules!

    It's a good practice to do even when they work without new, and recommended by several style guides and related tooling. Btw, since ES6 Error is subclassible, and its subclasses will require new.

    0 讨论(0)
提交回复
热议问题