> From: Brendan Eich <bren...@mozilla.com>
> Date: July 19, 2011 6:09:17 GMT+02:00
> To: Rick Waldron <waldron.r...@gmail.com>
> Cc: es-discuss <es-discuss@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

a...@rauschma.de
twitter.com/rauschma

home: rauschma.de
blog: 2ality.com



_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to