On Jul 18, 2011, at 9:02 PM, Rick Waldron wrote:

> Hey Bob, FWIW...
> 
> 
> class Point {
>   constructor(x, y) {
>     this.x = x;
>     this.y = y;
>   }
> 
>   zero() {
>     return new Point(0, 0);
>   }
> 
>   unit() {
>     return new Point(1, 1);
>   }
> 
>   prototype {
>     manhattanDistance() {
>       return Math.abs(this.x) + Math.abs(this.y);
>     }
>   }
> }
> 
> ...That's actually the nicest, most intuitively designed "class" structure 
> I've seen so far.

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


Notice how constructor is in the prototype object, as in JS with constructors 
and prototypes. Also, prototype: {...} not prototype {...}.

As Bob noted, though, even in JS and moreso (from Allen's stats) in Smalltalk, 
class methods are uncommon compared to prototype methods. Do you really want an 
extra level of indentation for the latter?

This example is skewed away from norms by having two class methods to one 
prototype method. I think constructor was mislocated, and when I fixed it just 
above, the instance- vs. class-method counts matched. Still unrealistic, though.

If we want *more* magic from the class syntax, not less, we could use 'new' at 
the outer class-element indentation level to declare the constructor, and 
automagically impute 'constructor' in the prototype from it. But we'd still be 
over-indenting the prototype methods, which are many, compared to the class 
methods (few). What was that Spock said about the needs of the many?

/be

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

Reply via email to