I'm trying to run this code and be able to "save" an object inside the "memory" array to view the past properties that the same object had after it changes.
this is the class:
class Circle {
radius;
center = {};
memory = [];
constructor(radius, x, y) {
this.radius = radius;
this.center.x = x;
this.center.y = y;
moveX(steps) {
if (!isNaN(steps)) {
this.saveMemory(this);
this.center.x += steps;
}
return this.center.x;
}
saveMemory(circle) {
let temp = (({ memory, ...rest }) => rest)(circle);
this.memory.push(temp);
}
when I view the memory array it shows the object after the x change.
var circle1 = new Circle(5, 1, 1);
circle1.moveX(2); // memory = [{radius:5,center{x:3,y:1})
How can I save the object before the changes were made?
edit: I want to get:
circle1.moveX(2); // memory = [{radius:5,center{x:1,y:1})