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

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


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.

- bob

On Wed, May 18, 2011 at 5:07 PM, Rick Waldron <waldron.rick at gmail.com>wrote:

> Just sort of curious... Given the following:
>
> // old and busted
> function SomeClass() {}; SomeClass.prototype.someMethod = function(arg) {
> ... }
>
> // new hotness
> class SomeClass { someMethod(arg) { ... } }
>
> In the second form, how would I denote a method that is defined as an own
> property? Additionally, how would something like the following be handled?
>
> function Ctor() {
>     this.method = function() {
>         return "own property";
>     };
>     return this;
> }
>
> Ctor.prototype.method = function() {
>     return "instance property";
> };
>
> var c = new Ctor();
>
> console.log(
>     c.method() // "own property"
> );
>
> Thanks in advance :)
>
> Rick
>
>
>
> On Wed, May 18, 2011 at 3:10 PM, Bob Nystrom <rnystrom at google.com> wrote:
>
>> On Wed, May 18, 2011 at 11:48 AM, Brendan Eich <brendan at mozilla.com>wrote:
>>
>>> The whole of class declaration is a mouthful. 'function' and 'prototype'
>>> are overlong too.
>>>
>>
>> Agreed. One of the reasons I'm really excited about this proposal is that
>> it addresses those:
>>
>> // old and busted
>> function SomeClass() {}; SomeClass.prototype.someMethod = function(arg) {
>> ... }
>>
>> // new hotness
>> class SomeClass { someMethod(arg) { ... } }
>>
>>
>> We can try for conciseness in this one corner but it doesn't help in
>>> general. I think we need to weight the whole syntax and see where the
>>> verbosity *and other* pain points are in practice.
>>>
>>
>> Sure. I'm not stuck on "new", but in the chunks of sample code I've put
>> together using "constructor" (which is what Traceur uses/did use for a good
>> while) it did actually stick out as a syntactic wart, at least to me.
>>
>> Same with 'constructor', and that property must exist somehow. Why not be
>>> explicit and avoid having two names for one prototype property?
>>>
>>> 3 is false in ES5. Reserved identifiers are valid property names. This is
>>> implemented in all the ES5-supporting browsers.
>>>
>>
>> Ah, my mistake. These are both fair points. It's too bad "constructor" is
>> such a long word.
>>
>> In the interests of considering all possibilities, one option would be to
>> use no name at all for the constructor:
>>
>> class Point {
>>   (x, y) {
>>     this.x = x;
>>     this.y = y;
>>   }
>> }
>>
>> I find that simultaneously delightfully lightweight and terrifyingly
>> obscure.
>>
>> - bob
>>
>>
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110518/0d7d06ff/attachment.html>


More information about the es-discuss mailing list