B.3.1 The __proto__ pseudo property

Andrea Giammarchi andrea.giammarchi at gmail.com
Tue May 21 09:58:32 PDT 2013


consider this then, same thing JSON is doing now in FF and Safari

```
var obj = Object.defineProperty({}, '__proto__', {
  enumerable: true,
  writable: true,
  configurable: true,
  value: []
});

console.log(obj instanceof Array); // false
obj.__proto__ = Array.prototype;
console.log(obj instanceof Array); // false

for(var key in obj) console.log(key); // "__proto__"

console.log(JSON.stringify(obj)); // {"__proto__":[]}
console.log(JSON.parse('{"__proto__":[]}') instanceof Array); // false
```

Above example would not even exist in a world where such magic property is
not present so, once again, I understood a while ago reasons is there but,
once again, I believe `Object.setPrototypeOf` is much more needed than such
property for real world cases where you test `if (Object.isExtensible(obj))
{ /* setPrototypeOf */ }` instead of `if
(!Object.prototype.hasOwnProperty.call(obj, "__proto__") || !(obj
instanceof Object) && Object.getOwnPropertyDescriptor(Object.prototype,
"__proto__").set) { try { setter.call(obj, proto) } catch (o_O) {
alert("setter was poisoned") } }`

Best Regards



On Tue, May 21, 2013 at 9:47 AM, Brandon Benvie <bbenvie at mozilla.com> wrote:

>  On 5/21/2013 9:43 AM, Andrea Giammarchi wrote:
>
> On Tue, May 21, 2013 at 12:56 AM, Brendan Eich <brendan at mozilla.com>wrote:
>
>> Andrea Giammarchi wrote:
>>
>>> can I also suggest to analyze, if there's still any doubt left on a
>>> method VS a property yet, this piece of code if not highlighted before?
>>>
>>
>>  I do not understand what you mean here.
>>
>>
>  I mean that JSON, as part of the specs, needs to consider that "magic"
> property case, resulting into an `instanceof Object`, with an enumerable
> property that will show up in a `for/in` loop but it's not able to mutate
> the object.
>
>  ```
>  var obj = JSON.parse('{"__proto__":[]}');
> alert(obj instanceof Array); // false
> alert(obj["__proto__"] instanceof Array); // true
> obj["__proto__"] = obj["__proto__"];
> // or
>  for (var key in obj) {
>   obj[key] = obj[key];
>   // could be a generic
>   // clone operation
> }
>  alert(obj instanceof Array); // false
> alert(obj instanceof Object); // true
>  ```
>
>  Above kind of object is "not perfectly described in current specs" and
> is different from any other where the `__proto__` is the inherited and not
> own property.
>
>
> JSON is not a subset of JS [1] already. There's no reason why it has to
> follow a newly specified JS syntax rule.
>
> [1] http://timelessrepo.com/json-isnt-a-javascript-subset
>
> _______________________________________________
> 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/20130521/250ec230/attachment.html>


More information about the es-discuss mailing list