Scenario:
– fill object1
– copying the content of object1 to object2
– delete element from object1
Result now:
– both object1 and object2 have 1 element deleted…?!
Wished result:
– object1 should have 1 element less than object2
The code:
var object1 = new Object();
object1['key_one'] = 'value_1';
object1['key_two'] = 'value_2';
object1['key_three'] = 'value_3';
object1['key_four'] = 'value_4';
var object2 = new Object();
object2 = object1;
delete object1['key_three'];
What am I doing wrong?
Your code does not do any copying.
object2is simply a new, empty object. Try this:Now
object2is a copy ofobject1, but deleting properties fromobject1will not affectobject2.Hope that makes sense.