DEV Community

Discussion on: Can You Guess the Result of This JavaScript Code?

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

So I was a bit curious what would happen if an object was defined like {__proto__: p, x: 1, x: 2} and p had a setter for x, so I tested it real quick.

Turns out, even in that case, which is the only possible way I could think of to introduce side effects inside an object literal, JavaScript ignores the setter on the prototype and creates a data property on the new instance (which then also shadows the inherited accessor property from the prototype).

So no, as far as I can tell, a double assignment in an object literal will literally never have any semantic meaning.

Thread Thread
 
yolvangz profile image
Yolvan G. Zambrano

Can you show the code of the example?