u

Brendan Eich brendan at mozilla.org
Thu Mar 15 13:40:40 PDT 2012


Herby Vojčík wrote:
>>> class List (n) {
>>>   this. at arr = n === +n ? new Array(n) : [];
>>> }.{
>>>   at (i) {
>>>     i = +i;
>>>     if (i>=0 && i<this. at arr.length) { return this. at arr[i]; }
>>>     else throw "Out of bounds: "+i;
>>>   }
>>>   size () { return this. at arr.length; }
>>> }

[snip...]

>> List.{
>>   from (array) {
>>     var r = new this();
>>     r. at arr = array;
>>     return r;
>>   }
>> };
>>
>> This looks like a class-side extension (from is a "static method",
>> specifically an alternative constructor). So the class expression
>> evaluates to the prototype, but the class name when used as an
>> Identifier expression evaluates to the constructor? That's incoherent.
>
> That is coherent with "new Foo" - 'Foo is the class' means 'new Foo 
> returns new instance'.

Yes, but your first example, class List(n) {...} cited above at the very 
top, uses .{ to add what looks like prototype methods at and size. If 
class List(n){...} evaluates to the constructor then you're adding these 
to the constructor function, not to its prototype. You'd need

class List (n) {
   this. at arr = n === +n ? new Array(n) : [];
}.prototype.{
   at (i) {
     i = +i;
     if (i>=0 && i<this. at arr.length) { return this. at arr[i]; }
     else throw "Out of bounds: "+i;
   }
   size () { return this. at arr.length; }
}

And of course to make this work when used as an expression that should 
evaluate to the constructor function, tack on a .constructor at the end.

Was this just a mistake or are you trying to have class List(n){...} 
evaluate to the prototype while List later evaluates to the constructor? 
I am still confused by what you wrote and believe it does not hang together.

And that takes us around the hermeneutic cycle again, which was my point 
about classes not reaching consensus easily or by shotgunning the design 
space. You may hit something but not bring down the prize bird: the 
right not-too-minimal and not-too-maximal classes.

/be


More information about the es-discuss mailing list