ES6 problem with private name objects syntax

Maciej Jaros egil at wp.pl
Wed Jan 8 00:59:51 PST 2014


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;
      }
}
```

I understand the motivation - using just `this[myPrivate]` wouldn't work 
because it could be inconsisten when `myPrivate` is a string variable. 
If `myPrivate='abc'` then `this[myPrivate]` is equivalent `this.abc`... 
So that is the main reason Name objects were born, right?

BUT what is the point of having this new syntax if I need to predefine 
all private variables (also the ones used for methods)?

Instead of above I could just use (shorter, more intuitive, already works):
```
var myPrivate;
class Test {
      constructor(foo) {
           myPrivate = foo;
      }
}
```

I could also secure the scope which would still be shorter for more then 
one variable:
```
(function(){
var myPrivate, myPrivate2;
class Test {
      constructor(foo) {
           myPrivate = foo;
           myPrivate2 = foo.toString();
      }
}
})()
```

I'm probably missing some optimization points but I was unable to find 
them on ES Wiki. The only new thing is the Name object. I see no use 
case for it and it doesn't seem to be more readable then current solution.

I like `this at variable` is more readable (aviable in one of the 
proposals), but it sound wrong when you read it - this-at-variable is 
wrong (variable is at this, not the other way around). So to make it 
sound right one could use `this#variable` or maybe even 
`this##variable`. Because "hash-hash variable", or to be more exact 
"hush-hush variable" is exactly what we mean. And as "#" is already 
reserved then it seem like a perfect candidate. There is no clash with 
previous syntax and no `Name` objects are needed. If browsers or other 
JS-like engines want to use them internally they should still be able to 
do so (pre-parse new syntax and add Name objects as needed).

What I'm saying is - please consider dropping `Name` objects and use 
some new syntax (e.g. `this#variable`) to avoid clashes but make 
declarations more readable for humans.

Also note that `var someName = new Name(); ... this[someName]` is 
actually longer then syntax from previous proposal: 
`private(this).someName` (almost 2 times longer in some cases).

Regards,
Maciej Nux Jaros.


More information about the es-discuss mailing list