So I just ran into this issue, and while i've been working Javascript a while now, I didn't realize what seems to be an innate behavior and I'm hoping you guys can help explain when in this is the case. Consider this:
let a = {key1: [1,2,3]};
let b = a.key1;
b.pop(1);
console.log(a); // { key1: [ 1, 2 ] }
It appears that instead of b
becoming a copy of a.key1
it is infact passing a pointer to the original data. This causes any changes that I make to b
reflect up to a.key1
. So my question is where can I read more about this behavior, does this apply for copying solely objects
, or does it apply to other things (e.g.arrays
, strings
).
This is somewhat alarming to me considering code I've written in the past not being aware of this and leaving landmines for myself to find later...