【问题标题】:why is my javascript object method returning undefined?为什么我的 javascript 对象方法返回未定义?
【发布时间】:2014-01-09 16:00:54
【问题描述】:

我也是 javascript oop 和游戏编程的初学者(!)。 在这里,我用一种方法创建了一个游戏玩家。但该方法返回未定义。这是为什么?,

bobsGame = {};

bobsGame.player  = function(which){
  this.which = which;
  this.rollDice = function () {
    diceVal = Math.floor(Math.random() * 6 + 1);
    console.log(diceVal);
    return diceVal;
  }
}

var player1 = new bobsGame.player('player1');

然后在标记中……

$('#roll-dice-btn-1').click(function(){
  bobsGame.player1.rollDice();
});

【问题讨论】:

  • 这里有太多不连贯的东西。例如,bobsgame.player1 在哪里定义?你能建立一个工作小提琴来展示你的问题吗?

标签: javascript oop


【解决方案1】:

您的班级中没有bobsGame.player1,您只是为变量player1 实例化了一个新实例?

var player1 = new bobsGame.player('player1');

$('#roll-dice-btn-1').click(function(){
    player1.rollDice();
});

FIDDLE

【讨论】:

  • 这个。您当前没有在您创建的 player1 对象上调用 rollDice() 方法。
  • @egl - 那我叫什么?
  • 您的回答很好,指的是OP错误地调用了该方法。
【解决方案2】:

你的电话应该是player1.rollDice()like

  $('#roll-dice-btn-1').click(function(){
    player1.rollDice();
  });

您对 bobsGame 对象的 player 属性和您实际创建的 player1 对象感到困惑。

【讨论】:

    【解决方案3】:

    这可能会更好一点

    bobsGame.player1 = new bobsGame.player('player1');
    

    【讨论】:

    • 你为什么要将类实例化为自身的属性?
    • 或者完全替换 bobsGame
    【解决方案4】:

    bobsGame.player1 未定义,因为 player1 未在对象 bobsGame 中声明。

    如果您想在您的 bobsGame 对象中创建 player1 作为键,则必须改用 bobsGame.player1 = new bobsGame.player('player1');。所以你的代码看起来像: bobsGame = {};

    bobsGame.player  = function(which){
      this.which = which;
      this.rollDice = function () {
        diceVal = Math.floor(Math.random() * 6 + 1);
        console.log(diceVal);
        return diceVal;
      }
    }
    
    var player1 = new bobsGame.player('player1');
    

    否则,您可以使用:

    $('#roll-dice-btn-1').click(function(){
      player1.rollDice();
    });
    

    【讨论】:

      【解决方案5】:

      你可以玩这样的结构:

      bobsGame = function(opts) {
          this.player = opts.player;
          this.diceVal = 0;
      }
      
      bobsGame.prototype.rollDice  = function(){
          this.diceVal = Math.floor(Math.random() * 6 + 1);
      }
      

      在你的主文件中:

      var player1 = new bobsGame({player: 'player1'});
      $('#roll-dice-btn-1').click(function(){
          player1.rollDice();
          console.log(player1.diceVal);
      });
      

      JsFiddle

      【讨论】:

        【解决方案6】:

        你的小问题。它在您的班级中找不到bobsGame.player1。所以像这样改变,它会工作得很好..

        bobsGame.player  = function(which){
            this.which = which;
        
            this.rollDice = function(){
                diceVal = Math.floor(Math.random() * 6 + 1);
                console.log(diceVal);
                return diceVal;
            }
        }
        
        
             var player1 = new bobsGame.player('player1');
            player1.rollDice();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-12-10
          • 2019-01-21
          • 2012-01-05
          • 2021-11-15
          • 2021-11-10
          • 2013-06-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多