Proposal for a static constructor and static member variables

Logan Smyth loganfsmyth at gmail.com
Thu Jan 26 05:41:19 UTC 2017


So we're on the same page, in the active proposal list (
https://github.com/tc39/proposals) you'll find
* Public Fields: https://tc39.github.io/proposal-class-public-fields/
* Private Fields: https://github.com/tc39/proposal-private-fields

Public Fields includes a syntax for static properties that would convert
your example to something like

```
class Multiton {
    static instances = {};

    constructor(name) {
        if (Multiton.instances.hasOwnProperty(name)) throw `Multiton name
already used`;

        Multiton.instances[name] = this;
    }
}
```

though I don't know I'd personally do this in this instance, rather than
putting it outside the class, like

```
const instances = {};
class Multiton {
    constructor(name) {
        if (instances.hasOwnProperty(name)) throw `Multiton name already
used`;

        instances[name] = this;
    }
}
```

Keep in mind that if something is a "static" in the ES6 class method sense,
you will never be able to do `this.instances` to get it because the
property does not live on `this`, it lives on the constructor. I'm unsure
if the private fields proposal would allow `static #instance = {};` as a
static with `#instance[name] = this;` inside the constructor.

On Wed, Jan 25, 2017 at 8:40 PM, Brandon Andrews <
warcraftthreeft at sbcglobal.net> wrote:

> The initial proposal for discussion is below:
>
>
> https://github.com/sirisian/ecmascript-static-constructor
>
> I don't believe this conflicts with other proposals yet, nor any future
> proposals. Essentially it adds static members in a very compact syntax with
> minimal grammar changes required.
>
> What I foresee happening years from now is public, private, and static
> member syntax will be added. Much like how public members are defined in
> the constructor for the moment the static constructor would be the way to
> define static members of the class for now.
>
> Thoughts?
> _______________________________________________
> 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/20170125/f571f30c/attachment.html>


More information about the es-discuss mailing list