I have problem with returning the value of object property
function Obj() {
this.objects =
[
{
id: 0,
values: {
x: 10,
y: 10
}
},
{
id: 1,
values: {
x: 15,
y: 20
}
}
];
}
Obj.prototype.getOjects = function() {
return this.objects;
};
Obj.prototype.getModifiedOjects = function() {
var i, obj;
obj = this.getOjects();
for(i=0; i<obj.length; i++ ){
obj[i].values.x *= 2;
obj[i].values.y *= 2;
}
return obj;
};
var obj = new Obj(), modifiedObjects;
console.log(obj.getOjects()); // value of 0 => {x => 10, y: 10}, 1 => {x => 15, y: 30}
modifiedObjects = obj.getModifiedOjects(); // Do something with mofified objects
console.log(obj.getOjects()); // value of 0 => {x => 20, y: 20}, 1 => {x => 30, y: 40}
When I call the getModifiedOjects function, also change the values of objects property.
How to make the getOjects function to not return object property by reference?
Thanks.