Reputation: 7471
you can see that the ruleCounter property in object being inserted is 1 and after insertion in the array we can see that the ruleCounter property has value 1 Then I try to insert same object but this time ruleCounter has a value of 2 but magically in the array you can see that the value of ruleCounter in both objects in the array becomes 2
please help why the values of ruleCounter in different index of the array gets automatically updated
var droppedObjects = []; //this array will contain the list of rules dropped on the drop zone area.
$scope.onDropComplete1 = function(data, evt) {
ruleCounter++;
data.ruleCounter= ruleCounter;
console.log(data);
//var index = $scope.droppedObjects.indexOf(data);
if (data !== null) {
droppedObjects.push(data); //droping data into the array when drag and drop is complete
console.log(droppedObjects);
} else {
//console.log($scope.droppedObjects1);
}
};
Upvotes: 2
Views: 654
Reputation: 3426
In javascript complex objects are passed by reference. This mean that if you have:
var objectA = {/* your properties */};
var objectB = objectA;
Both variables hold a reference to the same object. Thus if you change a property in using any of those variables you are changing the value in the other variable at the same time. You will need to create copies of the object if you want to keep different states.
var objectA = {/* your properties */};
var objectB = new Object(objectA); // or Object.create(objectA)
Now both are different object and you can change their properties independently.
Upvotes: 1