Often when passing an object as a parameter, functions may access the object by reference and make changes to the original object. This can sometimes lead to unwanted outcomes. Is there a way to ensure that an object remains unchanged? I am aware of the Object.freeze()
method.
However, it is important to note that Object.freeze()
does not affect objects or arrays nested within the main object.
For instance:
x = { arr: [1,2,3]};
Object.freeze(x);
x.arr.push(4); // this will still work