I have a global JSON variable where I store some parameters and then each time I run the function I want to be able to modify them but just locally inside that function.
So every time I run the function I want a fresh copy of the global variable inside the local one.
The problem is that I copy the global variable to a local one defined in the function, and I make changes to the local variable, but the next time I run the function, instead having an intact copy of the global variable, I am having the one where I have already modified things.
Thanks! 🙂
var test = {"name":"me"};
function bla() {
var t=test;
t.name="you";
t.age=55;
alert(test.name); // Returns "you" that have been set locally instead of "me" that was global value.
}
bla();
You need to clone the object, so that you can modify the clone and leave the original intact… What is the most efficient way to deep clone an object in JavaScript?