ES6 problem with private name objects syntax

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

On Wed, Jan 8, 2014 at 3:59 AM, Maciej Jaros <egil at> 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 ``... 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;
// 1

s = "s";
// undefined

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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list