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

Bob Nystrom rnystrom at google.com
Thu May 19 17:03:14 PDT 2011


On Thu, May 19, 2011 at 3:08 PM, Brendan Eich <brendan at mozilla.com> wrote:

> class Point {
>   x = 0;
>   ...
> }
>
> is a bit of a mystery.
>
> Is it an assignment expression-statement? No, no statements as *
> ClassElements*.
>
> Could it be creating a prototype property, as other unprefixed property
> initialisers such as get and set accessor declarations, and property
> initialiser extensions declaring prototype methods, do? Yes, that's the
> intent.
>
> But, I object (mildly), it does not *look* like a property initialiser. It
> looks like an assignment expression-statement.
>

Agreed, completely. My first stab at this used var for declaring fields:

class Point {
  var x = 0; // x field on prototype
}

I know Mark wishes var could be banished from the language completely. My
goal would be to have let become the thing you use for variables and
varthen becomes a "free" keyword we can then repurpose for something
like this.
I don't know if that would just sow confusion, but var seems like too nice
of a keyword to go to waste, and it would be unambiguous inside a class
since a class body doesn't allow arbitrary statements.


> But this is only for defining data properties on the class prototype. Is
> this case common?
>

Closure code uses data-on-the-prototype pervasively for default values,
like:

class Thermostat {
  var temp = 70; // on proto
  setTemp(temp) {
    this.temp = temp; // once you touch it, it becomes specific to the
instance
  }
}

I count nine examples of this just in
Control.js<http://closure-library.googlecode.com/svn/docs/closure_goog_ui_control.js.source.html>
(search
for "prototype.content_"). It has the advantage of not wasting per-instance
memory for values that are still the default, which is a neat trick that
class-based languages can't do.

Or if you have a measured need based on some existing code, which I'd love
> to hear about: consider a prefixing keyword to make this look like an
> alternative to static x = 0.
>

That was part of my motivation for using var. It seemed weird to have an
adjective like static without a noun that it modifies. (I was also using
function for methods for the same reason, though we've moved away from
that.)

- bob
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110519/80fd47aa/attachment.html>


More information about the es-discuss mailing list