On 19 January 2015 at 22:41, Fabrício Matté <[email protected]> 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
> [email protected]
> https://mail.mozilla.org/listinfo/es-discuss
>
>
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss