Proposal: Class Templates

T.J. Crowder tj.crowder at farsightsoftware.com
Thu Jan 17 08:24:04 UTC 2019


On Wed, Jan 16, 2019 at 5:29 PM ViliusCreator
<viliuskubilius416 at gmail.com> wrote:
> You could use
>
> ```js
> new Abc`abc`(‘abc’)
> ```

I think you must have missed [my earlier reply](
https://esdiscuss.org/topic/proposal-class-templates#content-7), which
covered that, and also the fact that the major problem with using `<>` for
this is that it's already valid syntax (in a common use case) meaning
something else (something almost nonsensical, but valid nonetheless).

On Wed, Jan 16, 2019 at 5:41 PM ViliusCreator
<viliuskubilius416 at gmail.com> wrote:
> Also, using `new (Abc(4, 5, 6))(1, 2, 3)` causes error ` Uncaught
TypeError: Abc(...) is not a constructor`.

Not if `Abc` is written correctly, for instance like Michał Wadas did with
`SizedArray` earlier in this thread. `Abc` example:

```js
const Abc = (a, b, c) => {
    // ...optionally do something with a, b, and c...
    // Return a class
    return class Abc$ {
        constructor(x, y, z) {
            this.x = x;
            this.y = y;
            this.z = z;
        }

        // ...optionally do something with a, b, and c...
        show() {
            console.log(`a = ${a}, b = ${b}, c = ${c}`);
            console.log(`x = ${this.x}, y = ${this.y}, z = ${this.z}`);
        }
    };
};
const obj = new (Abc(4, 5, 6))(1, 2, 3);
obj.show();
```
https://jsfiddle.net/gh3dv297/

-- T.J. Crowder
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20190117/c392c150/attachment.html>


More information about the es-discuss mailing list