0

I have a function PublicGame which I'd like to be using similar to a class. When I create PublicGame I give it a bunch of methods by setting this.methodName = function. The only thing is that I want to call some of these methods when the PublicGame is created. Right now for instance I do this.judge = this.setJudge(), but I know this wont work where I have it because, setJudge isnt defined yet. Should I put this at the bottom of PublicGame? Is my design totally off?
Code:

'use strict';
// var GameSockets = require(‘GameSockets’);
var Games = {};
var id_counter = 0;
var minPlayers = 3;
var maxPlayers = 6;
function PublicGame (players) {
 this._id = id_counter++;
 this.players = players;
 this.gameSocket = new GameSockets.registerPlayers(this.players, this._id, this.playerDisconnects);
 this.judge = this.setJudge();
 this.killGame = function() {
 delete Games[this._id];
 };
 // When a player presses leave game
 this.playerExits = function(playerToRemove) {
 // Delete player from players array
 this.players.splice(this.players.indexOf(playerToRemove),1);
 // If less than min players
 if (this.players.length < minPlayers) this.killGame();
 // If less than max players
 if (this.players.length < maxPlayers) {
 this.needsPlayers = true;
 }
 gameSockets.kickPlayer(playerToRemove);
 };
 // When a player disconnects without warning, e.g. closes window
 this.playerDisconnects = function(playerToRemove) {
 // Delete player from players array
 this.players.splice(this.players.indexOf(playerToRemove),1);
 // If less than min players
 if (this.players.length < minPlayers) this.killGame();
 // If less than max players
 if (this.players.length < maxPlayers) {
 this.needsPlayers = true;
 }
 };
 this.selectJudges = function() {
 this.judge = this.players.pop();
 this.players = this.players.unshift(this.judge);
 };
 this.setWinner = function(winner) {
 this.winner = winner;
 };
 Games[this._id] = this;
}
asked Oct 10, 2015 at 17:00
2
  • maybe you should read about javascript prototype and inheritance Commented Oct 10, 2015 at 17:09
  • I see no setJudge function anywhere. Commented Oct 10, 2015 at 17:12

2 Answers 2

1

If you define your functions on the prototype than you do not need to "wait" for the functions to be defined because the instance will already have them when the constructor's code is called

function PublicGame (players) {
 //...
 this.judge = this.setJudge();
}
PublicGame.prototype.killGame = function(){
 //...
};
PublicGame.prototype.playerExits = function(playerToRemove){
 //...
};
PublicGame.prototype.setJudge = function(){
 //do whatever
 return whatever;
};

So unless your functions need to access some "private" variable (ie defined within the constructor, not a global variable), or other reason requiring it, define it on the prototype instead of defining it in the constructor and it will be ready to use.

answered Oct 10, 2015 at 17:10
Sign up to request clarification or add additional context in comments.

2 Comments

So the protoypes would not be able to access anything inside of PublicGame? Would they be able to access players?
They wouldn't be able to access anything defined locally in the constructor, ie like var somevar = 3. You will be able to access players because you have defined it on the instance this.players = players
0

You have to use javascript prototype !

Read the comments in the code sample.

/*
* utils functions
*
* dont take care about that
**/
var el = document.getElementById('dbg');
var jj = function(val,sep){return JSON.stringify(val , null , sep || '')}
var log = function(val){el.innerHTML+='<div><pre>'+val+'</pre></div>'};
var counterId = 0;
/************************************************************************/
// You have to use prototype
// here an example of what you can achieve
// we create a Player 'class'
var Player = function( name ){ 
 this.id = counterId ++; //<-- an attribute
 this.name = name; //<-- an attribute
 this.setLevel(5);//<-- a method called at 'instanciation'
 
 return this;
};
// a method available at instanciation time
Player.prototype.setLevel = function(level){
 this.level = level;
 return this;
};
// we create a new Player named Toto
var Toto = new Player('Toto');
 log('Toto = ' + jj(Toto));//<-- utility function just to log
// we create a new Player named Jane
var Jane = new Player('Jane');
 log('Jane = ' + jj(Jane)); //<-- utility function just to log
// we change the Level of Jane
Jane.setLevel(12);
log('Jane.setLevel(12)');//<-- utility function just to log
log('Jane = ' + jj(Jane));//<-- utility function just to log
<div id='dbg'></div>

answered Oct 10, 2015 at 17:18

2 Comments

What is this? How does this help OP? It uses nothing of OP's code, and has no explanation to go with it
Sorry but it is in relation with my first comment with OP !

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.