0

I have a Game class that initializes player objects. I'm initializing two players p1 and p2. Yet when I console log them, they return the same settings, even though I'm setting p2's color to #000.

What's the problem?

I've tried this.p1.color and this.p1['color']

var Game = {
players: 2,
p1: {},
p2: {},
currentPlayer: 1,

initPlayers: function(){
    this.p1 = Player;
    this.p2 = Player;
    this.p1.credits = 500;
    this.p2.credits = 800;
    this.p1['color'] = '#3e3e3e';
    this.p2['color'] = '#000';
    console.log(this.p1);
    console.log(this.p2);
}

}

 var g = Game;
 g.initPlayers();

Console.log is showing

 Object {credits: 800, moves: Array[0], color:'#000', getMoves: function, checkPlayer: function, enterMove: function…}
 Object {credits: 800, moves: Array[0], color:'#000', getMoves: function, checkPlayer: function, enterMove: function…}

Even though I'm setting them separately above.

Clearly the setting of the values is working, both with brackets and dot notation, however, it's as if only the final values make it. And copy to both p1 and p2.

Am I missing something super obvious?

1
  • Btw, Game is not a class, but prototype. Until ES6 comes out, JS doesn't have classes.
    – Pavlo
    Dec 26, 2013 at 11:44

2 Answers 2

2

When you set

this.p1 = Player;
this.p2 = Player;

You make both variables hold the same object. I'm not sure of what you want so here are two hypothesis :

1) If you want to have separate copies of the Player object, you should clone them, for example like this (for a non deep cloning) :

this.p1 = {};
this.p2 = {};
for (var k in Player) {
   this.p1[k] = Player[k];
   this.p2[k] = Player[k];
}

2) If Player is a class, as the capitalized name would suggest, then what you need is an instanciation, which is done using new :

this.p1 = new Player();
this.p2 = new Player();
6
  • but doesnt invoking Player just instantiate a new Player from my Player Class??
    – Kylie
    Dec 26, 2013 at 11:31
  • 1
    @KyleK No, to instanciate you need to use the new operator. Dec 26, 2013 at 11:32
  • ok, if I do that, it says Object is not a function....do I need an init function or what?
    – Kylie
    Dec 26, 2013 at 11:33
  • It means Player is not a constructor. Your question was very incomplete... you should have included the relevant code... Dec 26, 2013 at 12:08
  • Guy above seemed to help me just fine with what I gave him?? Sorry....Im new to the whole JS object thing
    – Kylie
    Dec 26, 2013 at 12:12
1

In JavaScript when you assigning something to an object (e.g. this.p1 = Player), you don't create new object, but only link to the existing one. You'll need to create a fresh object, using Player as prototype:

this.p1 = Object.create(Player);
this.p2 = Object.create(Player);

Similarly, when you create new game you, better use Object.create() if you want to have more than one:

var g = Object.create(Game);
2
  • Thanks, I now have a problem, where Im storing moves in these objects. And moves are just x,y coords. I want each user to have their own moves[] array. However as I store the moves with this.moves.push, and access each seperate players, moves[] array, they show me both players cumulative moves.??? As if both objkects are accessing the same moves[] array??? Even though theyr initialized empty in the Player object when its created;
    – Kylie
    Dec 26, 2013 at 12:02
  • @KyleK this is a new question, I believe.
    – Pavlo
    Dec 26, 2013 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.