In my code snippet I have an object "cat" that I store into two variables "Pelle" and "Kalle". If I change the name of Pelle, it also changes the name of Kalle. I believe it has to do with them being references to cat.
How can I avoid this?
How can I change the value of Pelle without changing the name of Kalle?
Is the problem how I store the values in myFunction, or is the problem when I change the name in modify-function?
var cat = {
name : "Melvin",
age: : "9",
eyeColor : "yellow"
};
var pelle;
var kalle;
myFunction(cat);
modify();
print();
function myFunction(c) {
pelle = c;
kalle = c;
}
function modify() {
pelle.name = "Watts";
}
function print() {
document.getElementById("demo").innerHTML = pelle.name + " " + kalle.name;
}
Output from print() is Watts Watts.
Desired output is Watts Melvin.