1function func(obj) {
2 obj = JSON.parse(JSON.stringify(obj)); //If too slow, replace with other method of deep cloning
3 obj.a += 10;
4 return obj.a;
5}
6
7var myObj = {a: 5};
8func(myObj); //Returns 15 and myObj.a is still 5
9
10
1//Normal variable, No.
2function square(x) {
3 x = x * x;
4 return x;
5}
6var y = 10;
7var result = square(y);
8console.log(y); // 10 -- no change
9console.log(result); // 100
10
11//Objects like struct sub variables, Yes.
12function turnOn(machine) {
13 machine.isOn = true;
14}
15
16var computer = {
17 isOn: false
18};
19
20turnOn(computer);
21console.log(computer.isOn); // true;