Sylvestaro
Sylvestaro

Reputation: 101

Is it possible to change an objects constructor values after they have already been defined?

In javascript, suppose I have made some class like this:

class Player {

  constructor(playerName, playerScore, playerTurn) {

    this.playerName = playerName;
    this.playerScore = playerScore;
    this.playerTurn = playerTurn;

  }

}

Then I create a player:

playerOne = new Player('Bob',0,false);

Is it possible to change playerOne's constructor information somehow? For example, suppose I want to change 'Bob' to 'Alice', but not create a new object. Is this possible?

Upvotes: 0

Views: 29

Answers (1)

Barmar
Barmar

Reputation: 780724

After the constructor is done, it's just an ordinary object, you can read and assign the properties normally.

playerOne.playerName = "Alice";

Upvotes: 3

Related Questions