I am facing a dilemma due to Js referencing objects.
Within my Js "class," I have created a new player with various variables, strings, arrays, and functions to check specific conditions related to those variables.
As part of my code, I am running a while loop that ends when one player is declared dead. After this loop finishes, I want to reset the instantiated classes so I can run the loop again within a nested while loop.
For example:
function Player(name)
{
this.name = name;
this.hp = 100;
this.isnotdead = function()
{
return hp > 0;
}
}
var player1 = new Player('player1');
var player2 = new Player('player2');
while(war_is_happening)
{
while(player1.isnotdead || player2.isnotdead)
{
fight(player1, player2); // one of the players is killed
}
declareWinner(); // one player emerges as the winner, prompting another round of combat
resertPlayers() // My intention is to restart the nearest while loop with fresh variables for player 1 and player 2
}
I hope this explanation is clear.
In the past, I utilized
JSON.parse(JSON.stringify(player1))
to create a copy of the object. However, this method is no longer effective since my object (class) now contains internal methods.