After using that combine function in your original code, this is the output:
{ x: 1, y: 2, z: 3 } { x: 1, y1: 2, z1: 3, y: 2, z: 3 }
With that code, what ended up happening is the combine function overwrote the x property in b since that's what your function told it to do if you don't want to overwrite an already existing property, you have to use an if statement that checks to see if the property is undefined before assigning a's property to b.
Code:
var a = { x:1,y:2,z:3};
var b = {x:2,y1:2,z1:3};
function combine(a,b)
{
for(prop in a)
{
if(b[prop] == undefined)
{
b[prop] = a[prop];
}
}
return b;
}
combine(a,b);
console.log(a,b);
This is the output:
{ x: 1, y: 2, z: 3 } { x: 2, y1: 2, z1: 3, y: 2, z: 3 }