I noted some open issues on "Classes with Trait Composition"

Luke Hoban lukeh at microsoft.com
Thu May 19 15:00:51 PDT 2011


>> 2) The conventional JS pattern is to place methods on the prototype, not the instance, and many felt that the main thing classes need to provide is a syntax to make this traditional semantics easier to express.

>This is the big one.

Class syntax with class parameters instead of explicit constructors can still be defined to produce methods on the prototype, right?

Andreas' example:

  class Point(x, y) {
   public function abs() { return Math.sqrt(x*x, y*y) }
  }

Can effectively desguar to:

  function Point(x,y) {
    this.x = x; 
    this.y = y;
  }
  Point.prototype.abs = function() { return Math.sqrt(this.x*this.x, this.y*this.y); }

Or, with private names, to a variant with the captured x and y stored as private fields on the instance.  It's really just a syntax choice between explicit constructor functions and parameterized class definitions.  There are many benefits to the latter, and many modern OO languages seem to be using this style effectively.  

Luke



More information about the es-discuss mailing list