The class operator: a bridge between object and function exemplers

Allen Wirfs-Brock allen at
Mon Nov 14 17:04:04 PST 2011

On Nov 14, 2011, at 4:05 PM, Jake Verbaten wrote:

>>    UnaryExpression :
>>            class UnaryExpression
>>            ...
>> The semantics are:
>>  1. if UnaryExpression is undefined or null, return the value of UnaryExpression.
>>  2. Let obj be ToObject(UnaryExpression)
>> 3. Return the result of calling the [[Get]] internal method of obj with argument 'constructor'
> Interesting, so 'constructor' will inherit from Object.prototype if missing from the exemplar. This means
> let Point = class {
>      x:0,
>      y,0
> };
> let p = new Point(1, 2);
> will result in p being constructed via the equivalent of new Number(1).

sh*t! I though I'd had a solution for this, but maybe I don't...

If you are really going to define either an object or class exemplar, you really need to define a constructor.  The object exemplar case actually works out ok in this case  because the inherited constructor is "called as a function", not "as a constructor".   So for 

   let Point = {
     x: 0,
     y: 0
 let p = new Point(1,2);

Is pretty much equivalent to:
  let p = Point <| {};

which may not be exactly what the programmer expected  but it is a least in the vicinity and yields an object with (inherited) x and y properties.

> When you create a function, it has an automatically created prototype object who has a constructor property defaulted to the original function.
> Why would it be bad to augment the UnaryExpression after the class keyword so that it has an empty constructor if it doesn't exist? 

that would turn class from a simple property accessing operator to a mutating operator that I'm sure would never be accepted.  

A pattern like:
   class { }
should always have a constructor specified in the object literal but saying the class operator (as I'm trying to define it) adds a constructor seems very unhygienic.  Of course, it would be great if my IDE, or even my ES engine warned me when I coded this. Maybe that's enough.  But I suspect that it isn't.  I'm going to have to put some more thought into the own vs inherited constructor part of the design.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list