Herby Vojčík herby at mailbox.sk
Thu Mar 15 14:17:30 PDT 2012

Brendan Eich wrote:
>> 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.

I am probably writing densely and you had little time. I have written at 
the beginning of 1.:
'class ...}' as a sugar for 'function ...}.prototype'
(I put similar texts describing the idea to the header of 2. and 3. as well)

So, exactly, I wanted class List () { ... } work exactly the same as 
function List () { ... }.prototype - so List is the function but 
completion value is its prototype.

Which lets do the

class List (..) {
   // instance methods here

which is the most used case, but needs

   // static ones here

and is not exactly fine for class-expressions. But 1. is very minimal, 
leaving hard cases to be harder, but since it is only slightly-modified 
function (and user can by the time already learned enough to understand 
that 'class' is just a sugar), user can very easily switch class keyword 
to function keyword when it fits better for the situation. This 'class 
as function except returning prototype' is really useful primarily for 
declaring classes (and as a black magic for learners until they are 
later told class is nothing special). Just hinting it as one alternative 
of minimal 'class' use. Maybe it inspires something.

The other ones use 'class' differently.

> 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.

Maybe one of the alternatives (I like the 2. even when it needs do 
expressions when you want full-fledged class expression) can strike a 
chord with some of the readers.
1. is super-minimal (even when it returns the prototype) as its plus.

> /be

More information about the es-discuss mailing list