JavaScript class for a GameRoom I am new to object oriented design with JavaScript and am trying to make a PublicGame class with some methods that puts itself in a map of games. It calls killGame on itself if there aren't enough players. I feel like my design is way off. I won't be able to test my design though until the rest of my group members finish their parts, so I could use some feedback.

Here's a link to my state diagram. Everything in yellow is the game.

'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 &lt; minPlayers) this.killGame();

        // If less than max players
        if (this.players.length &lt; 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 &lt; minPlayers) this.killGame();

        // If less than max players
        if (this.players.length &lt; 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;
}
You're not using pseudo-private properties by way of trapping vars inside the constructor. Thus, there is no incentive to putting the methods on the constructor. It only increases your memory usage because the functions are created per instance and not shared across instances. Suggesting you move them out to the prototype instead.

function PublicGame(){...}
GameRoom.prototype.playerExits = function(){...};
GameRoom.prototype.playerDisconnects = function(){...};
// and so on




this.killGame = function() {
    delete Games[this._id];
};


Make sure you actually destroy the game properly, and not just remove its reference from the Games object. Make sure no handlers are referenced, no sockets running etc. Otherwise, the GC will never pick them up.



function PublicGame (players) {

  this.players = players;

  this.playerExits = function(playerToRemove) {

    this.players.splice(this.players.indexOf(playerToRemove),1);


In JS, we usually assume stuff because of the lack of type information and rely on the engine throwing an error when something is executed with the wrong data. 

However, in this case, you're assuming players is an array but never check if it really is during instance creation. The problem will come in later when you start doing array operations. In this case, splice will throw, but only later when a player exits. Then you scratch heads, lose hair and learn that your game has been running with broken data.

Guard players. Make sure it's an array and throw early before your code starts to churn with potentially broken data.



this.selectJudges = function() {
    this.judge = this.players.pop();
    this.players = this.players.unshift(this.judge);
};


You don't really need to pop off the last player to be the judge. Just put the judge in the beginning of the array to begin with and just pick him out using:

this.judge = this.players[0];




this._id = id_counter++;


Wondering if a counter would be a good game ID. What if you start to persist games across app restarts? Or run multiple instances of the game in parallel? They'll all start with 0 and numbers will collide. I suggest you use something like a GUID. They're usually unique and chances of collision are rare. Here's one from StackOverflow.