admin管理员组

文章数量:1429086

I'm a beginner in both javascript oop and game programming too(!) . 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();
});

I'm a beginner in both javascript oop and game programming too(!) . 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();
});
Share Improve this question edited Jan 9, 2014 at 16:03 Andy 63.6k13 gold badges71 silver badges98 bronze badges asked Jan 9, 2014 at 16:00 TopTomatoTopTomato 5853 gold badges8 silver badges24 bronze badges 1
  • 2 There are too many incoherent things in here. Where is defined bobsgame.player1 for example ? Can you build a working fiddle demonstrating your problem ? – Denys Séguret Commented Jan 9, 2014 at 16:03
Add a ment  | 

6 Answers 6

Reset to default 3

There is no bobsGame.player1 in your class, you just instanciated a new instance to the variable player1 ?

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

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

FIDDLE

Your call should be player1.rollDice() like

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

You are confused between player property of bobsGame object and the player1 object you actually created.

this might work a little better

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

bobsGame.player1 is undefined because player1 was not declared within the object bobsGame.

If you want to create player1 as a key in your bobsGame object, you must use bobsGame.player1 = new bobsGame.player('player1'); instead. So your code would look like: 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();
});

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

slight problem with your. It cannot find bobsGame.player1 in your class. 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();

本文标签: oopwhy is my javascript object method returning undefinedStack Overflow