In the following code, I create a constructor named Foo, then create a new object called zzz that uses that constructor and is called with reference to another variable.
function Foo(bar) {
this.a = bar;
}
const bar = [10, 10, 10];
const zzz = new Foo(bar);
console.log(zzz.a[0]);
bar[0] = 0;
console.log(zzz.a[0]);
This prints out 10, 0. I would expect that zzz.a is initialized as a copy of bar, but I found that changing bar will change the zzz object as well. Can someone explain why this happens?