Proposal: Storage for getters and setters
Dean Landolt
dean at deanlandolt.com
Fri Sep 30 08:17:58 PDT 2011
On Fri, Sep 30, 2011 at 10:14 AM, Xavier MONTILLET
<xavierm02.net at gmail.com>wrote:
> module name from "@name";
> let key = name.create();
> function MyClass(privateData) {
> this[key] = privateData;
> }
> MyClass.prototype = {
> doStuff: function() {
> ... this[key] ...
> }
> };
>
> All it does is make the key a special string...
>
No, the key is a special object...
> If you get the same string, you can still access the property...
>
Because it's an object this is impossible, and it's guaranteed to be
unforgeable.
> Plus, either you make it non-enumerable so that noone can find the key
> which is annoying, or you can find the key with a simple for in
> loop...
>
There's a note about a possible "visibility flag" extension in there, but as
it stands, no, it's not enumerable. The creator of the private name object
is the one that doles out the capability to get to the value.
> I'm not sure I understand this proposal...
>
Maybe it could use more example use cases. What you're proposing above could
also be done by keeping the private value in a closure, but if you need to
hang it off the object this gives you a way to do that safely.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110930/9184bc5a/attachment.html>
More information about the es-discuss
mailing list