Private fields in sub-objects within class definition.
# Michael Theriot (4 years ago)
Why stop at class definitions?
e.g.
let obj;
{
let local = 0;
obj = {
get local() {
return local;
}
}
}
becomes
const obj = {
#local: 0,
get local() {
return this.#local;
}
}
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>
# #!/JoePea (4 years ago)
Sure, but I'm first curious about classes because in my example, the code is all defined inside a class definition's lexical scope, where private fields (or keys) are currently defined to exist, so it seems like an approach we can currently take.
Private fields on objects without classes being in play would be neat too!
#!/JoePea
Sure, but I'm first curious about classes because in my example, the code is all defined _inside_ a class definition's lexical scope, where private fields (or keys) are _currently_ defined to exist, so it seems like an approach we can currently take. Private fields on objects without classes being in play would be neat too! #!/JoePea On Sun, Aug 9, 2020 at 7:55 PM Michael Theriot <michael.lee.theriot at gmail.com> wrote: > > 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
Not sure if the following is exactly how we'd want it to be, but it would be useful:
End user:
We could currently do something like this:
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