Message from JavaScript discussions

November 2018

— So why this works and the other no?


Consider this:

let a = {};
const b = { a };
const c = { a };

a = 'hello';
// should b.a be set to 'hello', or c.a? Or both? or none?

— The correct answer is none

— One sec let me see

— Why? It should

— Because I'm assigning to b, the reference of a

— Nope, you're not assigning to b

— In the opposite case, I have to deep copy the object to leave the copied object away.

— Ah OK. Sorry.

— When you do a = 'hello' you re

— Usually if you need to change a deep property you should deep copy the object, yes

— Passing a value of primitive type