Can `new` be optional?

Andrea Giammarchi andrea.giammarchi at gmail.com
Mon Nov 6 00:42:46 UTC 2017


> Why does calling a class throw an exception instead of just creating a
new instance

backward compatibility and early "rushed" behavior is one of the reasons.

`String() instanceof String` is false and it's just one example.

Because of primitives and `typeof`, `new Symbol` throws too, you are not
meant to do that.

Array and Regexp though, have no primitive namespace, but that's old gotcha
later specs tried to prevent.

What about `new Date` ? Now that's an instance, while `Date()` is a string.

Using `new` makes your intent unambiguous in a PL where functions can be
used as constructors too.

There is also the possibility to create functions that cannot be used as
`new`:

```js
const method = {method(){}}.method;

new method; // throws
```

Accordingly, since you have Reflect for all procedural use cases, what is
the benefit of not using `new` where it's an instance of that Class you are
after?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20171105/97dc5061/attachment.html>


More information about the es-discuss mailing list