Classes: suggestions for improvement

Axel Rauschmayer axel at rauschma.de
Sun Jun 12 15:43:30 PDT 2011


>> (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;
   }
   equals(p) { return this. at x === p. at x && this. at y === p. at y; }
   ...
 }

Maybe something like the following would work, too:

private.x = x;
...
return this.private.x === p.private.x;

-- 
Dr. Axel Rauschmayer

axel at rauschma.de
twitter.com/rauschma

home: rauschma.de
blog: 2ality.com



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110613/eaee528f/attachment-0001.html>


More information about the es-discuss mailing list