Re: EcmaScript Proposal – Private methods and fields proposals.
Waldemar Horwat
waldemar at google.com
Fri Apr 13 22:26:43 UTC 2018
On 04/13/2018 01:38 AM, Sultan wrote:
> The proposal is an explainer with regards to an alternative sigil-less syntax to back private fields/methods.
>
>>What does private(this)[property] do?
>
> "private(this)[property]" and alternatively "private[property]" or "private.property" all invoke access of a private "property" on the "this" instance of the class, symmetrical to thesyntax/function nature of both the "super" and"import" keywords.
>
>>How do private fields come into existence?
>
> Unless i've misunderstood what is meant by "come into existence" the proposals makes use of the reserved "private" keyword to define private fields i.e "private id = 1".
I was asking about what creates those fields.
>>What's private about private fields?
>
> Outside of a private fields provider class, private fields/methods would not be accessible.
>
>>How do you prevent them from being forged or stuck onto unrelated objects?
>
> What do you mean by this?
Writing your private field to an object that's not an instance of your class.
class A {
private id = ...;
private foo = ...;
write(value) {
private(this)["id"] = value;
private(this)["foo"] = ... my private secret that anyone outside the class must not learn ...;
}
}
and then invoking the above write method with a this value that's not an instance of A, such as a proxy.
Waldemar
More information about the es-discuss
mailing list