Proxy .__proto__ is not getPrototypeOf?

Andrea Giammarchi andrea.giammarchi at gmail.com
Mon May 5 16:57:07 PDT 2014


but that's exactly what I've replied to you already?

Once again, `__proto__` is spec'd in the `Object.prototype` like this:

```javascript

Object.defineProperty(
  Object.prototype,
  '__proto__',
  {
    configurable: true,
    get: function () {
      return Object.getPrototypeOf(this);
    },
    set: function (proto) {
      Object.setPrototypeOf(this, proto);
    }
  }
);

```

the only exception to that specification is as literal syntax so that
`{__proto__:[]}` is instanceof Array but `{"__proto__":[]}` is not ... that
is the only magic about the property, everything else passes through the
`Object.prototype`

Best Regards




On Mon, May 5, 2014 at 1:36 PM, John Barton <johnjbarton at google.com> wrote:

> 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
>>
>
>
> _______________________________________________
> 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/31ddddd5/attachment.html>


More information about the es-discuss mailing list