Proposal About Private Symbol

Gary Guo nbdd0121 at
Sat Dec 20 18:37:08 PST 2014

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.<br><br>```js<br>var constructor=function(){<br>    var privateField=Symbol('private', true);<br>    var pubField=Symbol('public');<div>    var leakedField=Symbol('leak', true);<br>    return function something(arg){<br>        this[privateField]=arg;<br>        this[pubField]=arg;</div><div>        this[leakedField]=arg;</div><div>        this["leak"]=leakedField;<br>    }<br>}<br>var arg="A";<br>var instance=new constructor(arg); // there is no way to access private fields<br>arg===instance[Object.getOwnPropertySymbols()[0]] // True, since Object.getOwnPropertySymbols expose the symbol</div><div>arg===instance[instance.leak]; // True, since instance.leak expose the symbol<br>```</div><div><br></div><div>Under careful use of the symbols, and without Object.getOwnPropertySymbols leaking every symbol, we can use symbols as private field.</div><div>V8 already has implemented private Symbol (just one more boolean field when defining the struct symbol) though it is not exposed to Script-side.<br><br>----------------------------------------<br>> From: d at<br>> To: waldron.rick at; nbdd0121 at; es-discuss at<br>> Subject: RE: Proposal About Private Symbol<br>> Date: Sat, 20 Dec 2014 20:11:04 +0000<br>><br>> For more reasons on why a simple "private symbol" approach does not quite work, see<br>><br></div> 		 	   		  

More information about the es-discuss mailing list