Private fields in sub-objects within class definition.
Michael Theriot
michael.lee.theriot at gmail.com
Mon Aug 10 02:55:01 UTC 2020
Why stop at class definitions?
e.g.
```js
let obj;
{
let local = 0;
obj = {
get local() {
return local;
}
}
}
```
becomes
```js
const obj = {
#local: 0,
get local() {
return this.#local;
}
}
```
On Fri, Aug 7, 2020 at 3:33 PM #!/JoePea <joe at trusktr.io> wrote:
> Not sure if the following is exactly how we'd want it to be, but it
> would be useful:
>
> ```js
> class Foo {
> // calculatedValue is intended to have read-only properties
> calculatedValue = {
> #x: 0,
> get x() { return this.#x },
> #y: 0,
> get y() { return this.#y },
> #z: 0,
> get z() { return this.#z },
> }
>
> update() {
> this.calculatedValue.#x = 42 // ok
> this.calculatedValue.#y = 42 // ok
> this.calculatedValue.#z = 42 // ok
> }
> }
> ```
>
> End user:
>
> ```js
> const foo = new Foo
> foo.calculatedValue.x // ok
> foo.calculatedValue.#x // syntax error
> ```
>
> We could currently do something like this:
>
> ```js
> class Foo {
> #calcX = 0
> #calcY = 0
> #calcZ = 0
>
> // calculatedValue is intended to have read-only properties
> calculatedValue = (() => {
> const self = this
> return {
> get x() { return self.#calcX },
> get y() { return self.#calcY },
> get z() { return self.#calcZ },
> }
> })()
>
> update() {
> this.#calcX = 42 // ok
> this.#calcY = 42 // ok
> this.#calcZ = 42 // ok
> }
> }
> ```
>
> Any plans for something like this? Is there a plan for private fields
> for object literals? If so, maybe that can somehow tie into usage
> within class bodies with WeakMap-ish semantics.
>
> #!/JoePea
> _______________________________________________
> 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/20200809/77e464e6/attachment.html>
More information about the es-discuss
mailing list