Proxy .__proto__ is not getPrototypeOf?

John Barton johnjbarton at google.com
Mon May 5 13:36:44 PDT 2014


Thanks, Boris, this explanation solves the puzzle for me.


On Mon, May 5, 2014 at 1:32 PM, Boris Zbarsky <bzbarsky at mit.edu> wrote:

> On 5/5/14, 4:21 PM, John Barton wrote:
>
>> Let me rephrase my question: why is the Proxy get even called in this
>> case?
>>
>
> Because the way __proto__ works is as if the JS implementation had done:
>
> (function() {
>   protoGetter = Object.getPrototypeOf;
>   Object.defineProperty(Object.prototype, "__proto__",
>     {
>        set: function() { /* magic here */ },
>        get: function() { return protoGetter(this); }
>     });
> })();
>
> before any page script got to run.
>
> Which means that __proto__ is a simple accessor property on
> Object.prototype.  On the one hand, that means that by default it appears
> on all objects.  On the other hand it means it can be shadowed, for example
> by someone doing an explicit defineProperty for that property name on some
> object.
>
> But it can also be shadowed by proxies, because those get to intercept
> _all_ property access.  So when a obj.__proto__ get happens the
> implementation walks up the proto chain of "obj" looking for a proxy or an
> object with an own property named "__proto__".  If a proxy is found, its
> get() is invoked and that's all there is to do as far as the implementation
> is concerned.  If an own property named "__proto__" is found, then the
> implementation checks whether it's an accessor or value property, and
> either returns the value or calls the getter, depending on which sort it is.
>
> -Boris
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140505/fd47e9a5/attachment-0001.html>


More information about the es-discuss mailing list