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
6 Answers
Reset to default 3There 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
版权声明:本文标题:oop - why is my javascript object method returning undefined? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745543898a2662617.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论