Darien Valentine (2017-08-08T05:46:43.000Z)
For a class which is not intended to be subclassable, this can be done today
with `Object.preventExtensions()` or `Object.seal()`, depending on your
intent:

    class Foo {
      constructor() {
        this.bar = 1;
        this.baz = 2;

        Object.preventExtensions(this);
      }
    }

    const foo = new Foo();

    foo.bar = 3; // okay
    foo.qux = 4; // throws in strict mode

But this approach doesn’t work when subclassing is or may be in play. It’s
also not
directly possible with the decorator proposal as it stands today — but
there has
been discussion of it and it sounds like it’s something that’s on people’s
minds:

[2017 July 27](
http://tc39.github.io/tc39-notes/2017-07_jul-27.html#11ive-interaction-of-privacy-fields-and-decorators
)

> DE: Omitted features: instance finishers. Yehuda?
>
> YK: an instance finisher is a function that is executed at the end of
> instantiation of the class at any subclass level and passes at the
> instance. this is at the end of Reflect.construct. the use case is a
> decorator to confirm that all instances are frozen or sealed. Another:
> you want to register created instance into a map. The subclass provides
> the key, the superclass expresses that the instance should be registered.
>
> DE: instance finishers change how instances are created. It's
> complicated and so wants to separate it out.

...looking forward to this, too.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170808/c46aba3e/attachment.html>
valentinium at gmail.com (2017-08-08T05:48:13.886Z)
For a class which is not intended to be subclassable, this can be done today
with `Object.preventExtensions()` or `Object.seal()`, depending on your
intent:

    class Foo {
      constructor() {
        this.bar = 1;
        this.baz = 2;

        Object.preventExtensions(this);
      }
    }

    const foo = new Foo();

    foo.bar = 3; // okay
    foo.qux = 4; // throws in strict mode

But this approach doesn’t work when subclassing is or may be in play. It’s
also not
directly possible with the decorator proposal as it stands today — but
there has
been discussion of it and it sounds like it’s something that’s on people’s
minds:

[2017 July 27](http://tc39.github.io/tc39-notes/2017-07_jul-27.html#11ive-interaction-of-privacy-fields-and-decorators)

> DE: Omitted features: instance finishers. Yehuda?
>
> YK: an instance finisher is a function that is executed at the end of
> instantiation of the class at any subclass level and passes at the
> instance. this is at the end of Reflect.construct. the use case is a
> decorator to confirm that all instances are frozen or sealed. Another:
> you want to register created instance into a map. The subclass provides
> the key, the superclass expresses that the instance should be registered.
>
> DE: instance finishers change how instances are created. It's
> complicated and so wants to separate it out.

...looking forward to this, too.

---

Edit: replied before seeing Logan’s response, hence the repetition.