Can `new` be optional?

Michael Lewis mike at lew42.com
Sun Nov 5 12:38:21 UTC 2017


Is there a technical limitation, or just an opinion?

If I call an ES6 `class MyClass {}` without new:  `MyClass()`, it throws.
Just change this bit to do whatever `new` was supposed to do.  Problem
solved?

On Sun, Nov 5, 2017 at 6:01 AM, Michał Wadas <michalwadas at gmail.com> wrote:

> This was covered by "call constructor" proposal, but unfortunately, it was
> withdrawn.
>
> https://github.com/tc39/ecma262/blob/master/workingdocs/callconstructor.md
>
> Status: "Withdrawn: can be solved with decorators" -
> https://github.com/tc39/proposals/blob/master/inactive-proposals.md
>
> On 5 Nov 2017 12:55 pm, "Michael Lewis" <mike at lew42.com> wrote:
>
>> Why can't `new` be optional?
>>
>> I found this thread, https://esdiscuss.org/topic/ob
>> soleting-the-new-keyword, but it's so long (and 9 years old), I didn't
>> read much of it.  Can someone summarize the current status of this decision?
>>
>> On a side note, it seems the ES Steering Committee needs a wiki - a place
>> to document important decisions.  I feel like the MDN web docs would be a
>> good place to put all levels of documentation.  I'm going to make a new
>> thread about docs, it's so important...
>>
>>
>> Anyway, in my ES5 "classes", I use an `if (!(this instanceof Class))
>> return new Class()` (similar to what jQuery does) to avoid having to use
>> `new`.  I feel like there should be a way to build this into the language.
>>
>> I have a `View()` class that is invoked a lot:
>>
>> ```
>> View().append({
>>     one: View(...),
>>     two: View(View(...), View(...))
>> });
>> ```
>>
>> And, being forced to write `new View()` every time is actually the only
>> reason why I won't switch to native classes, at the moment.
>>
>>
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20171105/4e4396cd/attachment.html>


More information about the es-discuss mailing list