An "extend" operator is a natural companion to <|

Axel Rauschmayer axel at rauschma.de
Tue Jul 19 01:55:17 PDT 2011


> From: Brendan Eich <brendan at mozilla.com>
> Date: July 19, 2011 6:09:17 GMT+02:00
> To: Rick Waldron <waldron.rick at gmail.com>
> Cc: es-discuss <es-discuss at mozilla.org>
> Subject: Re: An "extend" operator is a natural companion to <|
> 
> This is actually close to a less magical syntax that flips around the "ClassElements" (immediate children of the class {...} container) to specify class methods. Here's a less sugared version:
> 
> class Point {
>   zero() {
>     return new Point(0, 0);
>   }
> 
>   unit() {
>     return new Point(1, 1);
>   }
> 
>   prototype: {
>     constructor(x, y) {
>       this.x = x;
>       this.y = y;
>     }
> 
>     manhattanDistance() {
>       return Math.abs(this.x) + Math.abs(this.y);
>     }
>   }
> }
> 


I like the above, but it probably makes sense to stay closer to the proposal:

class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }

  manhattanDistance() {
    return Math.abs(this.x) + Math.abs(this.y);
  }
  
  static {
    zero() {
      return new Point(0, 0);
    }
  
    unit() {
      return new Point(1, 1);
    }
  }
}

I wouldn’t mind a "static" before each class property, either, as those will become rarer with modules. The above also stays close to the status quo, because it is basically the prototype. I find it easy to understand and explain. The above is not much different from Allen’s syntax, and replacing the <& operator with "static". But it allows one to hide the gory details with syntactic sugar (i.e., one does not expose that those properties are actually added to the constructor). To me the class literal proposal looks like prototypes-as-classes, with desugaring to constructor-functions-as-classes.


-- 
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/20110719/08e51e88/attachment-0001.html>


More information about the es-discuss mailing list