On 04/17/2018 05:31 PM, Sultan wrote:
Do you limit classes to creating only the private fields declared in the class,
or can they create arbitrarily named ones?
Yes, just as you could write arbitrary named fields with the mentioned WeakMap
approach, for example –
[...] private[key] = value
[...] private(this)[key] = value
[...] registry.get(this)[key] = value
and retrieve arbitrary fields
[...]private[key]
[...]private(this)[key]
[...]registry.get(this)[key]
The full form is expr.#foo, where expr can be `this` or some other expression
appropriate in front of a dot. The #foo binds to the innermost enclosing class
that has a private field called foo. If expr doesn't evaluate to an instance of
that class, you fail and throw.
Is this what you meant?
class A {
#foo = 1
constructor() {
let self = this
this.B = class B {
constructor() {
self.#foo = 2
}
}
}
get() {
return this.#foo
}
run(program) {
return eval(program)
}
}
let a = new A()
let b = new instance.B()
What's instance? I assume you meant new a.B().
Would this return 1 or 2 or would the previous statement throw?
console.log(a.get())
This would produce 2.
Additionally would this work
a.run('this.#foo = 3')
I have no idea. It depends on how the details of the scope rules for private
would work.
Waldemar
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss