Function.create

David Bruant david.bruant at labri.fr
Tue Sep 27 12:30:52 PDT 2011


Le 24/09/2011 23:50, David Bruant a écrit :
> Le 24/09/2011 22:25, François REMY a écrit :
>> I really think Proxies provides all the functionnalities required to
>> implement the special border cases where a non-conventionnal behavior
>> is needed.
> I do not think they do on their own, especially when it comes to object
> identity (equality of objects after a call to Object.getPrototypeOf).
> Proxies "suffer" from the same invariants than the regular objects which
> is that Object.getPrototypeOf cannot lie (there is no trap for it).
>
> However, with proxies + redefining Object.getPrototypeOf + adding a
> custom Object.changePrototype(object, newProto) (which would /virtually/
> change the prototype of a proxy (not a regular object!)), it could be done.
Hi,

I just wanted to give a follow-up on that. I implemented it:
https://github.com/DavidBruant/HarmonyProxyLab/tree/master/MutableProtoObjects
----
var o = MutableProtoObject({a:1, b:2});
console.log(o.a, o.b); // 1, 2 (inherited)
       
o.b = 3;
console.log(o.a, o.b); // 1 (inherited), 3 (own)

Object.changePrototype(o, {a:2, b:4, c:1});
console.log(o.a, o.b, o.c); // 2, 3 (own), 1

delete o.b;
console.log(o.a, o.b, o.c); // 2, 4, 1 // shadowing of b is over.
----

* MutableProtoObject is an equivalent to Object.create (without the
second argument).

* Object.changePrototype(o, newProto) works as you'd expect.

* Only newly created objects with MutableProtoObject can have their
prototype changed dynamically (while __proto__ can change built-ins)

* There is no protection against prototype chain cycles (it wouldn't be
hard to implement though).

Also, I implemented another flavor of Forwarder (similar to [1]) which
explicitly calls Object.getPrototypeOf when needed (which allows me to
return a different value when needed). I have no opinion on whether it's
better or worse, but i just wanted to point it out.

David

[1] http://wiki.ecmascript.org/doku.php?id=harmony:proxy_defaulthandler


More information about the es-discuss mailing list