Classes: suggestions for improvement

Brendan Eich brendan at mozilla.com
Sun Jun 12 15:53:44 PDT 2011


On Jun 12, 2011, at 3:43 PM, Axel Rauschmayer wrote:

>>> (1) The syntax for private properties still is a bit clumsy, and @ feels like wasting a nice symbol, how about using private.foo instead of private(this).foo?
>> 
>> No, you need private(other).foo or shorter for things like
>> 
>>  class Point {
>>    constructor(x, y) {
>>      private x = x;
>>      private y = y;
>>    }
>>    equals(p) { return private(this).x === private(p).x && private(this).y === private(p).y; }
>>    ...
>>  }
>> 
>> BTW, @ is hardly "wasted" as shorthand, since the above is way too long and also requires reifying private data as an object, which we do not want (I mean, I believe no one wants).
> 
> What confuses me slightly is that it initially looks like private and public properties share the same namespace, but later don’t seem to. Maybe it would be better to make the different namespaces explicit:
> 
>  class Point {
>    constructor(x, y) {
>      private @x = x;
>      private @y = y;

We hardly need private, then.

The point of the declarative forms that start with keywords such as private is to declare, not express. In the equals method I wrote originally:

  equals(p) { return @x === p at x && @y === p at y; }

@ is a prefix and infix operator in expressions.


>    }
>    equals(p) { return this. at x === p. at x && this. at y === p. at y; }

We've been over this ground. Why require . as well as @? The prefix form for this@ is a win too.


> Maybe something like the following would work, too:
> 
> private.x = x;
> ...
> return this.private.x === p.private.x;

No, ES5 allows this.private, foo.private, etc. (reserved identifiers as property names).

/be


More information about the es-discuss mailing list