Ducks, Rabbits, and Privacy
bruant.d at gmail.com
Tue Jan 22 01:35:39 PST 2013
Le 22/01/2013 07:31, Benoit Marchant a écrit :
> Why can we express in a property descriptor the notion of writable,
> configurable or enumerable but not private?
Because strings are forgeable, meaning that someone you may not trust
can read in your code or guess (maybe with low probability) the name of
the property making it not-so-private-after-all.
> Also, could be off topic, but the fact that for a getter/setter foo
> property, you have to implement yourself a non-enumerable _foo
> property to actually have some storage, is not particularly convenient.
That's a terrible idea missing the point of accessors which are expected
to encapsulate the state they're dealing with, not force you to put it
at everyone's sight. You don't *have* to do that ...
> A solution to that would be welcome! If a local variable following the
> name of the property was added to the scope of the getter/setter while
> it's called on an object could be one way, it would certainly
> encourage following encapsulation rather than accessing a private
> property directly, which would still be possible.
... and you're providing the solution yourself, getters and setters can
share a variable in a common scope. The language can't decide to add its
own variable, because it could collide with or shadow an existing
variable making the code much harder to understand and reason about. So
you have to create the variable yourself.
Interestingly, if instead of a non-enumerable _foo property a private
symbol was used, getters and setters would be the property-wise
equivalent of proxies; the private symbol playing the role of the target
and the publicly exposed string property being the proxy.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the es-discuss