ES6 problem with private name objects syntax

Maciej Jaros egil at wp.pl
Mon Jan 13 15:15:54 PST 2014


Sorry for not answering sooner...

Rick Waldron (2014-01-08 23:24):
>
> On Wed, Jan 8, 2014 at 3:59 AM, Maciej Jaros <egil at wp.pl 
> <mailto:egil at wp.pl>> wrote:
>
>     To my understanding private name objects are supposed to make
>     private properties and functions available for new classes syntax
>     in ECMAScript 6 standard.
>
>     But the syntax is rather strange:
>     ```
>     var myPrivate = new Name();
>     class Test {
>          constructor(foo) {
>               this[myPrivate] = foo;
>          }
>     }
>     ```
>
>
> Private names were replaced by the not-private Symbol. Symbol is a 
> symbol, private if you keep it that way and public if you expose it.
>
>     I understand the motivation - using just `this[myPrivate]`
>     wouldn't work because it could be inconsisten when `myPrivate` is
>     a string variable. 
>
>
> Symbol produces symbols, not strings.
>
>     If `myPrivate='abc'` then `this[myPrivate]` is equivalent
>     `this.abc`... So that is the main reason Name objects were born,
>     right?
>
>
> If the symbol was used to create the property, and the binding 
> undergoes a reassignment, the property won't be accessible via 
> property access by bracket notation:
>
> var o = {};
> var s = Symbol();
>
> o[s] = 1;
> o[s];
> // 1
>
> s = "s";
> o[s];
> // undefined

So basically one could implement Symbol as something similar to GUID 
generator, right? It should work even if you simply restart a counter 
and make sure you don't clash with other stuff and e.g. return 
"__#symbol#__1", "__#symbol#__2" and so on.

> You could still get the Symbol by Object.getOwnPropertySymbols(o)... 
> again there is no implied privacy with Symbols.

And if you can easily get those symbols then why bother? Why not just 
use "o._s = 1;" (i.e. use some character as a convention to mark private 
members - as in Python).

I'm sorry for asking stupid questions, but I'm writing a thesis and just 
trying to understand what is the current state and where is this going 
to. My understanding was that the key goal at first was the readability 
of the code. This somehow got shifted to simplicity and minimalism. Not 
sure what the current goal is. It seems like members visibility in a 
traditional, declarative sense was just omitted and is shifting to using 
as less of new symbols and keywords as possible.

Regards,
Maciej Jaros.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140114/b5418a5e/attachment.html>


More information about the es-discuss mailing list