Conway's Game of Life - Conventional JavaScript? I'm using the Game of Life Kata to help me learn JavaScript. I've picked up the syntax through Codecademy tutorials, but my current skill level is Novice.

The example code has working functionality for initialising a World with live cells and asking it to 'tick'.

All suggestions for improvement are welcome. I'd especially like feedback on the following:


JavaScript idioms and conventions
Function and variable scoping
Making use of built-in functions and datatypes




myapp = {};

/**
 * World represents a 2 dimensional square matrix of cells.
 * @param dimension
 * @param liveCellCoordinates
 * @returns {myapp.World}
 */

myapp.World = function(dimension, liveCellCoordinatesArray) {
    this.dimension = dimension;
    this.liveCellCoordinates = liveCellCoordinatesArray;
};

/**
 * 
 * @returns {Array} of live cells after tick.
 */
myapp.World.prototype.tick = function() {
    var nextGenerationOfLiveCells = [];

// for each cell in world
for(var y=0; y&lt;this.dimension; y++) {
    for(var x=0; x&lt;this.dimension; x++) {

        var liveNeighbours = this.getLiveNeighbourCount(x, y);
        if(this.isCellAlive(x, y)) {
            if(isSurvivor(liveNeighbours)) {
                nextGenerationOfLiveCells.push([x,y]);
            }               
        }
        else {
            if(isBorn(liveNeighbours)) {
                nextGenerationOfLiveCells.push([x,y]);
            }
        }
    }
}

this.liveCellCoordinates = nextGenerationOfLiveCells;

return nextGenerationOfLiveCells;
};

myapp.World.prototype.getLiveNeighbourCount = function(x, y) {
var liveNeighbours = 0;

if(this.isCellAlive(x-1, y-1)) liveNeighbours++;
if(this.isCellAlive(x,   y-1)) liveNeighbours++;
if(this.isCellAlive(x+1, y-1)) liveNeighbours++;
if(this.isCellAlive(x-1, y)) liveNeighbours++;
if(this.isCellAlive(x+1, y)) liveNeighbours++;
if(this.isCellAlive(x-1, y+1)) liveNeighbours++;
if(this.isCellAlive(x,   y+1)) liveNeighbours++;
if(this.isCellAlive(x+1, y+1)) liveNeighbours++;


//  jstestdriver.console.log("&gt;&gt;&gt; liveNeighbours of ", x, y, ": ", liveNeighbours);

return liveNeighbours;
};

myapp.World.prototype.isCellAlive = function(x, y) {
    for(var cell in this.liveCellCoordinates) {     
        if(x === this.liveCellCoordinates[cell][0] &amp;&amp; y === this.liveCellCoordinates[cell][1]) {
            return true;
        }
    }
    return false;
};

function isBorn(liveNeighbours) {
    return liveNeighbours === 3;
}

function isSurvivor(liveNeighbours) {
    return liveNeighbours === 2 || liveNeighbours === 3;
}
Looks quite good overall; not much to criticise. (I'm not looking at the overall structure or logic - just the style; the game of life can be written in so many ways)

Anyway, what I have is very trivial:


Missing indentation in the bodies of tick and getLiveNeighbourCount
Don't use a for...in loop for arrays (in isCellAlive); better and clearer to use a regular for-loop like you do elsewhere.
You ought to define myapp as window.myapp. Right now it's implicitly global, because it's missing a var declaration, but you might as well make that explicit (or perhaps it doesn't need to be global at all?)
Some more comments in the code itself would probably be nice.