es-discuss Digest, Vol 95, Issue 45

Nick Krempel ndkrempel at google.com
Mon Jan 19 15:28:15 PST 2015


On 19 January 2015 at 22:41, Fabrício Matté <ultcombo at gmail.com> wrote:

> In that case, we can expect that users will want to implement "factory
>> when called" as a default behavior for their classes.  From that, we can
>> foresee that the following pattern:
>>
>>     if (!new.target) return new Whatever(...args);
>>
>> will become a kind of boilerplate.  Obviously, we want to avoid
>> boilerplate as a general rule.
>>
>
> +1!
>
> I personally consider `new` as user code pollution.
> I have been considering to use classes but export factories in my
> libraries and packages' code. The initial idea was:
>
> ```js
> class Foo {/*...*/}
>
> module.exports = function(...args) {
> return new Foo(...args);
> };
> ```
>
> But the sample above has obvious problems:
>
> 1. It becomes a tedious boilerplate to wrap every single class into a
> function export;
> 2. It can't be transpiled to ES5. Rest parameters are transpiled to
> `.apply()` which can only do [[Call]] and not [[Construct]].
>
>
Point 2 is incorrect (e.g. `new (Foo.bind.apply(Foo,
[undefined].concat(args)))`).


> (forgot to CC es-discuss before oops, let's see if this forward goes
> correctly now)
>
> /fm
>
> _______________________________________________
> 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/20150119/15131728/attachment.html>


More information about the es-discuss mailing list