0

I have problem with sharing the same property to all classes.

Look at my code simplified code: https://codepen.io/anon/pen/wqRBYL?editors=0112

I have main Class called "Game". Two next classes - "Coin" and "Monster" inherits from Game.

Game has this.coins property. When Coin will update this array I want to see it on Monster class. How should I write it?

// console.log
"Game object: " [1, 2, 3, 4, 5, 6]
"Coin object: " [1, 2, 3, 4, 5, 6, 7, 8]
"Monster object: " [1]
"Game object: " [1]
"---------"

And how can I prevent double Game log?

// edit: Code from CodePen:

// Main Class.
function Game() {

  this.coins = [1]; // I want to share this array between all classes that inherit from the Game

  var self = this;

  setInterval(function() {
    console.log('Game object: ', self.coins)  // Correctly shows values thar Coin class is adding.
  }, 5000)


}

//Inherits from Game.
function Monster() {

  Game.call(this);

  var self = this;

  setInterval(function() {
    console.log('Monster object: ', self.coins); // Always show first state of Game's array - [1]
  }, 5000)

}

Monster.prototype = Object.create(Game.prototype);
Monster.prototype.constructor = Monster;

//Inherits from Game.
function Coin() {

  Game.call(this);

  var self = this,
      newCoin = 2;

  setInterval(function() {
    self.coins.push(newCoin++)
    console.log('Coin object: ', self.coins); // Correctly returns more and more.
  }, 5000)

}

Coin.prototype = Object.create(Game.prototype);
Coin.prototype.constructor = Coin;

new Coin();
new Monster();

setInterval(function() {
    console.log('---------');
}, 5000)
  • @Andreas I believe this doesn't require more explanation. – binariedMe Aug 30 '17 at 15:57
  • see to share coin count to children, you must keep it in prototype like Game.prototype.coin and not associated with the Game's instance by doing this : this.coin – binariedMe Aug 30 '17 at 15:58
  • If you want to keep the shared value not under the prototype of the Game class (since everybody can access it) but private instead then i had come up with two separate ways. Have a look at [this](https://stackoverflow.com/a/35826569/4543207) answer of mine. – Redu Aug 30 '17 at 16:22

1 Answers1

0
// Main Class.
function Game(options) {
inherit from the Game

  var self = this;

  setInterval(function() {
    console.log('Game object: ', self.coins)  // Correctly shows values thar Coin class is adding.
  }, 5000)


}
Game.prototype.coin = 0; // this will be shared across Game and its children's instances.
//Inherits from Game.
function Monster() {

  Game.call(this);

  var self = this;

  setInterval(function() {
    console.log('Monster object: ', self.coins); // Always show first state of Game's array - [1]
  }, 5000)

}

Monster.prototype = Object.create(Game.prototype);
Monster.prototype.constructor = Monster;

//Inherits from Game.
function Coin() {

  Game.call(this);

  var self = this,
      newCoin = 2;

  setInterval(function() {
    self.coins.push(newCoin++)
    console.log('Coin object: ', self.coins); // Correctly returns more and more.
  }, 5000)

}

Coin.prototype = Object.create(Game.prototype);
Coin.prototype.constructor = Coin;

new Coin();
new Monster();

setInterval(function() {
    console.log('---------');
}, 5000)
binariedMe
  • 4,309
  • 1
  • 18
  • 34