Could someone pelase explain the mutation behaviour below:
function test () {
let data = {}
const run = () => {
console.log(data)
}
const update = () => {
data = {}
}
return {
container: {
data
},
run,
update
};
}
const { container, run, update } = test();
update(); // run an update to reset value to {}
container.data.test = { result: 'test' }; // try to mutate the data
run(); // it will still return {} instead { data: {test: {result: 'test'}} }
I would expect to update the object would result mutate the data object inside the function., but looks like it's not the case, as if it created a new object.