Proposal About Private Symbol

Gary Guo nbdd0121 at hotmail.com
Sat Dec 20 18:40:53 PST 2014




Oops, seems Outlook.com ruins my email. One more time

I don't think it's a problem though. As long as the private Symbol doesn't leak, there is no way to access private properties. Private Symbol as I supposed only eliminate itself from getOwnPropertySymbols, and that's it, there should not be no more constraints on private Symbol.

```js
var constructor=function(){
    var privateField=Symbol('private', true);
    var pubField=Symbol('public');
    var leakedField=Symbol('leak', true);
    return function something(arg){
        this[privateField]=arg;
        this[pubField]=arg;
        this[leakedField]=arg;
        this["leak"]=leakedField;
    }
}
var arg="A";
var instance=new constructor(arg); // there is no way to access private fields
arg===instance[Object.getOwnPropertySymbols()[0]] // True, since Object.getOwnPropertySymbols expose the symbol
arg===instance[instance.leak]; // True, since instance.leak expose the symbol
```

Under careful use of the symbols, and without Object.getOwnPropertySymbols leaking every symbol, we can use symbols as private field.
V8 already has implemented private Symbol (just one more boolean field when defining the struct symbol) though it is not exposed to Script-side.

----------------------------------------
> From: d at domenic.me
> To: waldron.rick at gmail.com; nbdd0121 at hotmail.com; es-discuss at mozilla.org
> Subject: RE: Proposal About Private Symbol
> Date: Sat, 20 Dec 2014 20:11:04 +0000
>
> For more reasons on why a simple "private symbol" approach does not quite work, see https://github.com/zenparsing/es-abstract-refs/issues/11&issuecomment-65723350
>
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20141221/50a362d6/attachment-0001.html>


More information about the es-discuss mailing list