Readdition of __proto__

Allen Wirfs-Brock allen at wirfs-brock.com
Mon Oct 14 17:32:38 PDT 2013


On Oct 14, 2013, at 4:21 PM, Andrea Giammarchi wrote:

> my last memories on the topic are these:
> 
> ```javascript
> 
> var obj = JSON.parse('{"__proto__":[]}');
> obj instanceof Array; // false
> obj.__proto__ instanceof Array; // true
> // since the proto is a property, not a magic thing
> obj.__proto__ = 123; // da hell will happen, only Allen knows ^_^
> 
> ```
> 
> And since latter should simply set the property named `"__proto__"` as value `123` I got confused with this dual way to deal with an object when it comes from JSON world (then has a property defined as value, not the inherited set/get from Object.prototype)
> 
> As summary, `JSON.parse` over `"__proto__"` is similar to:
> ```javascript
> 
> var o = {};
> Object.defineProperty(o, '__proto__', {
>   value: 123,
>   enumerable: true,
>   writable: true,
>   configurable: true
> });
> 
> ```
> 
> Which means in such case the property `"__proto__"` will fail with such object while `Object.setPrototypeOf` won't which is the reason I keep suggesting the latest to make the intent explicit.
> 
> Not arguing or anything, just speaking out loudly my confusion with that property as string part.
> 

I think you are over thinking this:

Assuming that Annex B.2.2.1 and B.3.1 are implemented: here are the cases of interest:

let o1 = {__proto__: p};      // o1 inherits from p
let o2 = {"__proto__": p};   // o2 inherits from p
let o3 = {["__proto__"]: p};// o3 inherits from Object.prototype, has own data property "__proto__" whose value is p.
let o4 = JSON.parse('{"__proto__": "value"}');
      //o4 inherits from Object.prototype, has own data property "__proto__" whose value is "value"

//assuming that Object.prototype.__proto__ has not been tamper with:
let o5 = new Object;
o5.__proto__ = p ; //o5 inherits from p

let o6 =new Object;
o6["__proto__"] = p; //o6 inherits from p

Allen


More information about the es-discuss mailing list