Object.assign interaction with __proto__ field.

Claude Pache claude.pache at gmail.com
Wed Sep 26 16:06:37 UTC 2018



> Le 26 sept. 2018 à 16:27, Mike Samuel <mikesamuel at gmail.com> a écrit :
> 
> Might it be a spec bug that in the below, o's prototype changes, and o.x !== b.x?
> 
> const a = makeIntercepter();
> const b = { x: 1 };
> const o = Object.assign(
>   {},
>   a,
>   b);
> 
> console.log(`o is plain Object: ${ Object.getPrototypeOf(o) === Object.prototype }`);
> 
> console.log(`b.x=${ b.x }, o.x=${ o.x }`);
> 
> function makeIntercepter() {
>   return JSON.parse(
>     // Get an object that has an actual "__proto__" property.
>     '{ "__proto__": {} }',
>     // Replace the __proto__ property's value with one that
>     // traps assignment to x.
>     (key, value) => (
>       (key === '__proto__')
>         ? {
>             set x(v) {
>               console.log(`intercepted ${ v }`);
>             },
>             get x() {
>               return 2;
>             },
>           }
>         : value));
> }
> 
> In modern Chrome, Firefox, Safari I get
> intercepted 1
> getPrototypeOf(o)===Object.prototype: false
> b.x=1, o.x=2
> 
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss <https://mail.mozilla.org/listinfo/es-discuss>

🤦 It’s not a bug. But you definitely convinced me to add `delete Object.prototype.__proto__` at the top of my JS.

—Claude

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180926/edad56f2/attachment.html>


More information about the es-discuss mailing list