简体   繁体   English

为什么我的javascript对象方法返回未定义?

[英]why is my javascript object method returning undefined?

I'm a beginner in both javascript oop and game programming too(!) . 我也是javascript oop和游戏编程的初学者(!)。 Here i've created a game player with a method. 在这里,我用一种方法创建了一个玩家。 but the method is returning undefined. 但该方法返回未定义。 why is that?, 这是为什么?,

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');

and then in the markup… 然后在标记中…

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

There is no bobsGame.player1 in your class, you just instanciated a new instance to the variable player1 ? 您的课程中没有bobsGame.player1 ,您只是实例化了一个变量player1的新实例?

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

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

FIDDLE 小提琴

Your call should be player1.rollDice() like 您的电话应该是player1.rollDice()类的

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

You are confused between player property of bobsGame object and the player1 object you actually created. 您在bobsGame对象的player属性和实际创建的player1对象之间感到困惑。

这可能会更好

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

You can play with a structure like that: 您可以使用以下结构:

bobsGame = function(opts) {
    this.player = opts.player;
    this.diceVal = 0;
}

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

And in your main file: 并在您的主文件中:

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

JsFiddle JsFiddle

slight problem with your. 您的小问题。 It cannot find bobsGame.player1 in your class. 它无法在您的课程中找到bobsGame.player1 So change like this and it would work fine.. 因此,像这样进行更改,它将可以正常工作。

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();

bobsGame.player1 is undefined because player1 was not declared within the object bobsGame . bobsGame.player1是不确定的,因为player1没有对象中声明bobsGame

If you want to create player1 as a key in your bobsGame object, you must use bobsGame.player1 = new bobsGame.player('player1'); 如果要创建player1作为bobsGame对象中的键,则必须使用bobsGame.player1 = new bobsGame.player('player1'); instead. 代替。 So your code would look like: bobsGame = {}; 因此您的代码如下所示: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');

Otherwise, you can use: 否则,您可以使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM