Could <| be spelled "extends"?

Allen Wirfs-Brock allen at wirfs-brock.com
Fri Feb 1 13:02:43 PST 2013


Friday afternoon dreaming...

Now that we have introduced class declarations, I occasionally discover situations where I need to create a singleton specialization of some class. One way to do that is to apply a new operator to an anonymous class expression.  For example,

let p = Proxy(target, new class extends VirtualObject {
      get(key, receiver) {...}
      set(key,value, receiver) {...}
});

Now this is a pretty heavy weight mechanism.  It creates both a constructor function and a prototype object in addition to the the actual instance object that is all I really want to create.

Back when we were considering the <| operator the above might have been expressed as

let p = Proxy(target, VirtualObject <| {   //the proposed semantics made VirtualObject.prototype the [[Prototype]] of the obj lit
      get(key, receiver) {...},
      set(key,value, reciever) {...}
});

Which is both more concise and which only creates a single instance object rather than a constructor/prototype/instance triad. Something like this can still be expressed in the current draft of ES6 as:

let p = Proxy(target, {
      __proto__:  VirtualObject.prototype,
      get(key, receiver) {...},
      set(key,value, reciever) {...}
});

This is ugly in its use of __proto__ and unreliable because __proto__ can be removed from Object.prototype.  Object.create is another ugly way to express something like this.

The class express idiom got me thinking about another alternative.  Note that "extends" has always been an ES FutureReservedWord.  That means that using "extends" without a preceding "class" keyword won't break any existing code. Perhaps we could express this example like:

let p = Proxy(target, extends VirtualObject {
      get(key, receiver) {...},
      set(key,value, receiver) {...}
});

"extends" that isn't part of a class declaration would be a prefix operator defined like:

PrimaryExpression ::
   ...
   extends Expression ObjectLiteral

a desugaring semantics might be:

Object.mixin(new (Expresion), ObjectLiteral)

Allen




More information about the es-discuss mailing list