The class operator: a bridge between object and function exemplers

Brendan Eich brendan at
Mon Nov 14 14:01:55 PST 2011

On Nov 14, 2011, at 12:16 PM, Allen Wirfs-Brock wrote:

> let Point = {
>      x:0,
>      y,0,
>      constructor(x,y} {
>          this.x=x;
>          this.y=y;
>      }
> }.constructor;   //<----------- note added property reference
> let p = new Point(1,2);  //new operator applied to a constructible function 

So can you spec operator new a bit? It tries [[Construct]] and if missing, tries for a .constructor property that it calls on a fresh instance of Object whose [[Prototype]] is the exemplar?

Dave's work on brought to light some added wiring that comes for free in the prototypal pattern when you write a constructor function as a function declaration: the intrinsic name of the constructor function is the class name.

For exemplars, if constructor is defined using method definition shorthand,

says the intrinsic name will be 'constructor'. This will be a drag in debugging scenarios. You'll want to see Point, Square, Circle, Ellipse, Rectangle, etc. but all you'll get is 'constructor' every time.

The attributes in the section are frosty. Is that the right default for methods? Is it the right one for constructor in particular? I'm just asking, this is a separate issue but I spotted it and wanted to get it out before forgetting.

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

let p = new Point(1, 2);

will result in p being constructed via the equivalent of new Number(1).

> The class operator  can prefix any UnaryExpression, not just object literals.  That means that the class operator can be used to classify objects:
> if (class p === Point) ...

This is good.

It has the same multiple-global issue that instanceof has.

> Note that many object-oriented abstraction designers consider this for of class testing to be an anti-pattern. 

Yup but it has its uses occasionally.

> Relationship between the class and instanceof operators
> There isn't one.  They are different operators.  class is simply a short hand for accessing an object's constructor property  instanceof tests a specific inheritance relationship. 

Oh, but weren't you going to make instanceof work with an object exemplar on the right?


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

More information about the es-discuss mailing list