Could <| be spelled "extends"?
Le 01/02/2013 22:02, Allen Wirfs-Brock a écrit :
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.
Deleting Object.prototype.proto affects the semantics of the object literal? This doesn't smell good.
PrimaryExpression :: ... extends Expression ObjectLiteral
a desugaring semantics might be:
Object.mixin(new (Expresion), ObjectLiteral)
I like this idea :-)
How about a new version of Object.create, e.g. Object.make? Roughly:
Object.make = function (proto, props) { return props ? Object.create(proto, Object.getOwnPropertyDescriptors(props)) : Object.create(proto); }; // with Object.getOwnPropertyDescriptors() having the obvious definition
Result:
let p = Proxy(target, Object.make(VirtualObject, { get(key, receiver) {...}, set(key,value, receiver) {...} }));
Going purely by gut feeling, I would put extends
in the realm of constructor inheritance and would be confused to see it used on objects. I’d expect extends VirtualObject
to be an anonymous class (=constructor).
Previously:
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)