对象的字段实例是否可以调用对象本身? (JavaScript)

Is it possible for a field instance of an object to call upon the object itself? (JavaScript)

TL;DR

如果一个对象 X 有一个对象 Y 作为它的字段实例,有没有办法让 Y 调用或检索 X 而无需将 X 指定为 Y 的字段实例?


我正在编写一个 JavaScript 程序来实现 Farkle,一个基于骰子的游戏。为了 运行 一款 Farkle 游戏,我实例化了 FarkleGame class 的一个实例。这涉及 FarklePlayer class 的两个实例化和 FarkleDie class 的实例化。两个 FarklePlayer 实例(代表玩 Farkle 的两个人)和一个 FarkleDie 实例(代表 Farkle 中使用的一个骰子)被指定为字段FarkleGame class 中的实例,并在 FarkleGame 构造函数中初始化。但是,两个 FarklePlayer 实例需要能够访问存在于 FarkleGame class 定义中但在它们自己的 [=34] 之外的数据=]FarklePlayerclass定义。

例如: 人类必须在 Farkle 中掷骰子,并且可能会根据掷骰的值获得分数。我希望 FarklePlayer 实例通过访问更高级别 FarkleGame 实例的 die 字段属性以及score_die() 方法。

这是一个示例控制流程:

  1. FarkleGame 实例询问 FarklePlayer 实例是否要通过她的回合或掷骰

  2. FarklePlayer 实例选择滚动,并调用她的 class 方法 roll()

  3. 但是FarklePlayer的class方法roll()实际上是检索属于上层FarkleGame实例的FarkleDie实例,并告诉FarkleDie实例调用 rolled(),并检索更高级别的 FarkleGame 实例并告诉它调用 score_die().

class FarkleGame {
  player_one;
  player_two;
  die;

  constructor(name_of_player_one, name_of_player_two) {
    this.player_one = new FarklePlayer(name_of_player_one);
    this.player_two = new FarklePlayer(name_of_player_two);
    this.die = new FarkleDie();
  }



  get_player_one() {
    return this.player_one;
  }
  
  get_player_two() {
    return this.player_two;
  }

  get_die() {
    return this.die;
  }

  score_die() {
    let die_value = this.get_die().get_value();
    let score = ((die_value * die_value) * 10) - 1);  
}


}


class FarklePlayer { 
  name;
    constructor(name_of_player) {
        this.name = name_of_player;

}
     roll() {
           // FarklePlayer instance wants to roll and needs to access the 
           // die field attribute and score_die() method of the 
           // higher-level FarkleGame instance of which it is a part of.

      higher_level_FarkleGame_instance.get_die().rolled();
      higher_level_FarkleGame_instance.score_die();
}

}


class FarkleDie {

  value;
  
  constructor() {
    this.value = null;
  }

  rolled() {
    let value_after_rolled = (Math.floor(Math.random() * 6) + 1);
    this.value = value_after_rolled;
  }
}


请务必注意,我不想将 FarkleDie 实例作为某些 FarklePlayer roll() 方法的参数传递。我希望 FarklePlayer roll() 方法访问更高级别的数据(即 FarkleGame 实例的字段实例),甚至指示其更高级别的实例做某事(通过调用方法定义 FarkleGame class).

下级实例如何调用所属上级实例的字段和方法?

提前致谢。

OP ...

It's important to note I don't want to pass the FarkleDie instance as a parameter for some FarklePlayer's roll method.

I want the FarklePlayer's roll method to access higher-level data (namely a field attribute of the FarkleGame instance)

从上面的评论...

"Since a player acts or has to act within the context of a game why does the OP not pass the very game (instance) to the player's constructor function at each player's instantiation time?"

那为什么不做显而易见的事情呢。一个游戏实例包含所有需要的引用。因此,player 确实通过其 game 引用访问了 die

class FarkleGame {
  constructor(firstPlayerName, secondPlayerName) {
    this.playerOne = new FarklePlayer(firstPlayerName, this);
    this.playerTwo = new FarklePlayer(secondPlayerName, this);
    this.die = new FarkleDie();
  }/*
  Why prototypal getters, since all
  the properties are public anyhow?

  getPlayerOne() {
    return this.playerOne;
  }
  getPlayerTwo() {
    return this.playerTwo;
  }
  getDie() {
    return this.die;
  }*/
}

class FarklePlayer { 
  constructor(name, game) {
    this.name = name;
    this.game = game;
  }
  roll() {
    this.game.die.rolled();
  }
}

class FarkleDie {
  constructor() {
    this.value = null;
  }
  rolled() {
    this.value = (Math.floor(Math.random() * 6) + 1);
  }
}

const game = new FarkleGame('Jill', 'Jack');

console.log(
  'game.die.value:',
  game.die.value
);

console.log(
  '... game.playerOne.roll() ...',
);
game.playerOne.roll();

console.log(
  'game.die.value:',
  game.die.value
);

console.log(
  '... game.playerTwo.roll() ...',
);
game.playerTwo.roll();

console.log(
  'game.die.value:',
  game.die.value
);
.as-console-wrapper { min-height: 100%!important; top: 0; }