My issue is with a property called 'tiles' in a class that holds information about the state of a checkers board game. Each time I make a legal move or start the game, I want to push this property to an array named 'moves'. However, the problem arises when pushing the new tiles property causes the previous elements in the moves array to change to the values of the most recently pushed tiles.
I realize this is happening because the object is passed by reference, which results in replacing old elements in the array since they all point to the same object - the latest value of the tiles property. Is there a way to push each different individual state of 'tiles', resulting from legal moves, rather than by reference as my code currently does?
Take a look at my snippet below: App.js
App = function () {
var self = this;
self.tiles = [];
// Objects are populated from a JSON file
// Code to fetch JSON and save it to self.tiles
// More code
this.startGame = function () {
// Other code
self.moves.push(self.tiles);
};
this.makeMove = function () {
// Other code
self.moves.push(self.tiles);
};
};
What I am expecting is for the objects in the self.moves array to point to different objects instead of the same one. The array should hold different states of self.tiles, but currently, every time I push the property, the elements in the 'moves' array are overwritten by the latest value of self.tiles.
Any assistance in resolving this issue would be greatly appreciated. Thank you!