Javascript 从对象构造函数之外的函数访问实例

Javascript 从对象构造函数之外的函数访问实例,javascript,jquery,oop,object,Javascript,Jquery,Oop,Object,我有一个问题,我不明白后,许多尝试解决它 为了帮助您理解,有两个类(游戏和棋盘)和第三个文件,其中包含jQuery按键控件。游戏是关于游戏的逻辑,而棋盘是关于显示 下面是我希望能够充分理解的代码的一部分 // GAME CLASS function Game(width, height) { this.width = width; this.height = height; this.forbiddenPosition = []; this.chartBoard = this

我有一个问题,我不明白后,许多尝试解决它

为了帮助您理解,有两个类(游戏和棋盘)和第三个文件,其中包含jQuery按键控件。游戏是关于游戏的逻辑,而棋盘是关于显示

下面是我希望能够充分理解的代码的一部分

// GAME CLASS
function Game(width, height) {
  this.width = width;
  this.height = height;

  this.forbiddenPosition = [];

  this.chartBoard = this.resetBoard();

  this.generateGame();
}

Game.prototype.generateGame = function () {
  this.player1 = new Player("Joueur 1", 100, dagger);
  this.player2 = new Player("Joueur 2", 100, dagger);
  const playerArray = [this.player1, this.player2];
}

Game.prototype.getPlayer1 = function () {
  return this.player1;
};

Game.prototype.getPlayer2 = function () {
  return this.player2;
};
Game.prototype.switchTurn = function (player1, player2) {

  console.log(player1);
  console.log(player2);
};

// BOARD CLASS
const ctx = $('#board').get(0).getContext('2d');

function Board (width, height) {
  this.width = width;
  this.height = height;
  this.game = new Game(this.width, this.height);

  this.displayInfoPlayers(this.game.getPlayer1(), this.game.getPlayer2());
}


Board.prototype.displayInfoPlayers = function (player1, player2) {
  $('.canvas-side__left').css('visibility', 'visible');
  $('.canvas-side__right').css('visibility', 'visible');
  $('.canvas-side__left').addClass('animated slideInLeft');
  $('.canvas-side__right').addClass('animated slideInRight');

  $(".canvas-side__left").html("<h2 class='canvas-side--title'>" + player1.name + "</h2><p class='canvas-side--health'>" + player1.health + "</p><p class='canvas-side--health'>" + player1.weapon.name + "</p>");

  $(".canvas-side__right").html("<h2 class='canvas-side--title'>" + player2.name + "</h2><p class='canvas-side--health'>" + player2.health + "</p><p class='canvas-side--health'>" + player2.weapon.name + "</p>");
};

// CONTROL
$(document).on('keypress', function (e) {
  if (e.which == 13) {
    Game.prototype.switchTurn(Game.prototype.getPlayer1(), Game.prototype.getPlayer2());
    e.stopPropagation();
  }
});
//游戏类
功能游戏(宽度、高度){
这个。宽度=宽度;
高度=高度;
this.forbiddenPosition=[];
this.chartBoard=this.resetBoard();
这个。generateGame();
}
Game.prototype.generateGame=函数(){
this.player1=新玩家(“Jouer1”,100,匕首);
this.player2=新玩家(“Jouer2”,100,匕首);
const playerray=[this.player1,this.player2];
}
Game.prototype.getPlayer1=函数(){
返回此文件。player1;
};
Game.prototype.getPlayer2=函数(){
把这个还给我。player2;
};
Game.prototype.switchTurn=函数(player1、player2){
控制台日志(player1);
控制台日志(player2);
};
//板级
const ctx=$(“#board”).get(0.getContext('2d');
功能板(宽度、高度){
这个。宽度=宽度;
高度=高度;
this.game=新游戏(this.width,this.height);
this.displayInfoPlayers(this.game.getPlayer1()、this.game.getPlayer2());
}
Board.prototype.displayInfoPlayers=函数(player1、player2){
$('.canvas-side__-left').css('visibility','visible');
$('.canvas-side__right').css('visibility','visible');
$('.canvas-side__left').addClass('animated slideInLeft');
$('.canvas-side__right').addClass('animated slideInRight');
$(“.canvas-side_uuleft”).html(“+player1.name+”

“+player1.health+”

“+player1.wearm.name+”

”; $(“.canvas-side\uu right”).html(“+player2.name+”

“+player2.health+”

“+player2.armor.name+”

”; }; //控制 $(文档).on('keypress',函数(e){ 如果(e.which==13){ Game.prototype.switchTurn(Game.prototype.getPlayer1(),Game.prototype.getPlayer2()); e、 停止传播(); } });
Board类链接到Game类,因此使用它。使用jQuery代码的控件位于第三个文件中,而不是类中

当我按Enter键时,player1和player2将处于未定义状态。我尝试了不同的方法来调用getter函数,但没有任何效果。我也试着把控件放在游戏文件中,但还是什么都没有

我得到未定义或getPlayer1()不是函数


我正在寻找一种从任何地方调用这些getter函数的方法,这样我就可以使用player1和player2,我需要在板上移动它们。

这里有几个问题

  • 按键事件处理程序正在使用
    Game.prototype
    ,而不是
    Game
    的实例。您希望使用您创建并存储在某处的
    游戏的实例<代码>游戏。原型
    没有
    player1
    player2
    属性。它们由
    Game
    构造函数添加到
    Game
    的实例中。没有任何东西会将它们添加到
    游戏中。prototype
    (这是正确的,它们不应该出现在prototype上)

  • 不需要
    getPlayer1
    等。您可以直接访问
    player1
    player2
    。(可以将
    player1
    player2
    设置为私有,并且只为它们提供访问器,但目前这有点复杂,可能还不是你想要的。)

  • Game
    方法中,您需要始终如一地使用
    this.player1
    this.player2
    ,不要让玩家四处走动

  • 对于
    Board
    来说,创建
    Game
    的实例似乎有些奇怪。看起来应该是相反的


  • 我建议从这个任务中退一步,首先尝试一些更简单的事情(比如创建一个类,一个类的实例,并在事件处理程序中使用该实例),然后逐步增加复杂性,确保在每个阶段都清楚发生了什么。当你去的时候,你可能会有更具体的问题,你可以在上面发布(经过彻底的研究等)。

    你可以这样做,它应该会起作用。从本质上讲,您要对您试图访问的函数进行原型化,该函数在构造函数之后才被声明

    class Test {
      constructor() {
        this.five = Test.prototype.getFive();
      }
    
      getFive() {
        return 5;
      }
    }
    
    let test = new Test();
    console.log(test.five); // Returns 5
    

    Game.prototype.getPlayer1()
    将返回
    undefined
    ,因为
    引用
    Game.prototype
    不是
    Game
    的实例。您需要使用新的操作符创建
    Game
    的实例,如
    const Game=new Game()
    ,然后运行
    Game.getPlayer1()
    etc@adiga但是该函数返回this.player1。这就是为什么我不理解这个问题。正如我在前面的评论中提到的,
    这个
    在调用
    Game.prototype.getPlayer1()
    时指的是
    Game.prototype
    。而且
    游戏中没有
    player1
    属性。它仅存在于使用创建的
    游戏
    对象上。它可能是:
    const game=new game();game.getPlayer1()
    @adiga它能用,谢谢。谢谢。所以如果我不将player1和player2设置为私有,那么getPlayer1现在就没用了?我试着以一种实用的方式工作,但我还在学习。我在棋盘上创建了一个游戏实例,因为显示器正在等待游戏(游戏逻辑)的更改,并且只显示它。@Grégoryhuikhe-对,根据您现在拥有的,
    player1
    getPlayer1
    具有相同的可见性,所以。。。(我应该注意,如果您试图以函数式编程的方式工作,您可能根本不会使用构造函数和
    new
    )我应该使用什么来代替构造函数和新操作符?函数式编程中没有类?@Grégoryhweighe-I sug