【问题标题】:How to share the same property for all classes that inherit from the parent如何为从父级继承的所有类共享相同的属性
【发布时间】:2017-08-30 15:53:57
【问题描述】:

我无法将相同的属性共享给所有类。

看我的代码简化代码:https://codepen.io/anon/pen/wqRBYL?editors=0112

我有一个名为“游戏”的主类。接下来的两个类 - “Coin”和“Monster”继承自 Game。

游戏有 this.coins 属性。当 Coin 更新这个数组时,我想在 Monster 类上看到它。应该怎么写?

// 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]
"---------"

以及如何防止双重游戏日志?

// 编辑:来自 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 我相信这不需要更多解释。
  • 看到分享硬币数量给孩子,你必须把它保存在像 Game.prototype.coin 这样的原型中,并且通过这样做不与游戏的实例相关联:this.coin
  • 如果你想保持共享值不在 Game 类的原型下(因为每个人都可以访问它)而是私有的,那么我想出了两种不同的方法。看看我的this 回答。

标签: javascript oop object inheritance


【解决方案1】:
// 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)

【讨论】:

  • 当然,我必须将它添加到原型中。它有效,谢谢! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-21
  • 1970-01-01
相关资源
最近更新 更多