When assigning objects in javascript how do we determine the properties already existing in the assigned object is not being replaced. For example say:
var foo = {
property1: "a",
property3: "d"
}
var bar = {
property1 : "b",
property2 : "c"
}
then
a = b
would replace property1 in foo with b. In the context I'm considering, we are not sure whether the property1 of foo is existing when the assignment happens. So when assigning the option is to check if property1 exists, if not only then assign the property1 from b. So simple a=b assignment is not an option. So is there a way to assign an object which will only append the properties which are not existing in the object assigned to, while not replacing the already existing ones.
I guess one way to do it would be to :
for(var prop in b){
if(!(a[prop])){
a[prop] = b[prop];
}
}
Is there a better shorthand way?
aandbare undefined in your example. Did you meanfoo = bar?