Ducks, Rabbits, and Privacy

Benoit Marchant marchant at mac.com
Thu Jan 24 10:20:21 PST 2013


Thanks Brendan, that make sense but that's more than what I think I'm looking for. What I'm looking for is a way to store a value in a object's property that can only be accessed from a property access stand point by the object itself. My only goal is to make sure that outside code can't break encapsulation for code robustness/quality reason.

Isn't it possible internally to allow a property access only by "this" ?

 Benoit

On Jan 24, 2013, at 10:10 AM, Brendan Eich <brendan at mozilla.com> wrote:

> Benoit Marchant wrote:
>> I guess I don't quite understand why it seems contentious to add a "private" property to property descriptors which already "reserve" properties like "value", "enumerable" or "writable".
>> 
>> "private" is a meta description of a property like "value", "enumerable" or "writable.
>> 
>> That feels a more natural extension than adding class to the language.
> 
> As David wrote, this does not work in a dynamic language.
> 
> function generic_get(obj, prop) {
>    return obj[prop];
> }
> 
> obj = {private foo: 42, get bar() { return this.foo; }}; // or equivalent class syntax
> 
> // elsewhere
> var steal = generic_get(obj, 'foo');
> 
> How do you enforce that only bar can access foo from obj? A private attribute on a property with a public string-equated name 'foo' does not help.
> 
> The mistake is treating name privacy as a property attribute. Privacy is not an attribute of property descriptors, it's a restriction on property names. It is not a static restriction in any sense, rather a capability. If you keep the private symbol or weak map confined, privacy is assured.
> 
> /be
> 



More information about the es-discuss mailing list