Fwd: es-discuss Digest, Vol 95, Issue 45

Fabrício Matté ultcombo at gmail.com
Mon Jan 19 14:41:23 PST 2015


>
> 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]].

(forgot to CC es-discuss before oops, let's see if this forward goes
correctly now)

/fm
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150119/1dd92b18/attachment.html>


More information about the es-discuss mailing list