ES6 problem with private name objects syntax

Rick Waldron waldron.rick at gmail.com
Wed Jan 8 14:24:05 PST 2014


On Wed, Jan 8, 2014 at 3:59 AM, Maciej Jaros <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


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


Rick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140108/a844ae7f/attachment.html>


More information about the es-discuss mailing list