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

Bob Nystrom rnystrom at google.com
Wed May 18 17:57:40 PDT 2011


On Wed, May 18, 2011 at 5:51 PM, Mark S. Miller <erights at google.com> wrote:

> On Wed, May 18, 2011 at 5:17 PM, Bob Nystrom <rnystrom at google.com> wrote:
>
>> I believe it would be something like:
>>
>> class Ctor {
>>   public method() {
>>     return "own property";
>>   }
>>
>>   method() {
>>     return "instance property";
>>   }
>> }
>>
>> By default, members within a class body are declared on the prototype.
>> Prefixing it with public places it on the new instance. Using public for
>> this isn't carved in stone yet, but <some keyword> means "place the
>> member on the new instance" and that prefix can be applied to any kind of
>> member: method, variable, constant, etc.
>>
>
> Not quite. Per-instance member initialization only happens in the
> constructor, because the values of per-instance properties is generally data
> dependent on constructor parameters. The "public" (or whatever) and
> "private" declarations only prefix ForwardDeclarations, which are generally
> just comma separated lists of identifiers. The initialization in the
> constructor happens in the traditional ES5 imperative manner. Also, the
> public list is optional. It serves only a documentary purpose. So....
>
>  class Ctor {
>   public method; // optional forward declaration of own property
>
>   method() {
>     return "instance property";
>   }
>
>   constructor() {
>     this.method = function() {
>       return "own property";
>     };
>   }
> }
>

Ah, OK. While it isn't necessary, it would be really convenient to be able
to create per-instance properties by providing an initializer in the class
body if those properties don't depend on "this" or ctor params:

class Point {
  public x = 0, y = 0;
}

let p = new Point();
p.x; // 0

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


More information about the es-discuss mailing list