Reputation: 45
I am working on coding a Conway's Game of Life grid.
I am new to JavaScript and I am trying to add a method to the board object that will return one cell
's location on the board. But I am getting an error telling me it's an invalid return statement
. Can you please explain what I am doing wrong?
Board.prototype = {
addCell: function(cell) {
this.cells[getCellRepresentation(cell.x, cell.y)] = cell;
}
getCellAt: function(x,y) {
return this.cells[getCellRepresentation(x,y)]
}
}
Upvotes: 2
Views: 7199
Reputation: 20842
The first thing I see is that you are missing a comma:
Board.prototype = {
addCell: function(cell) {
this.cells[getCellRepresentation(cell.x, cell.y)] = cell;
}, // <---- put a comma here
getCellAt: function(x,y) {
return this.cells[getCellRepresentation(x,y)]
}
}
The reason you need a comma is that the 2 functions are part of an initialization statement, and addCell and getCellAt are both members of the Board.prototype, and are initialized with anonymous function expressions which are members of an expression list. Consider JSON syntax.
var obj = {
name: "bob",
age: 21,
party: function() { ... }
}
If the functions were normal named functions, you might see:
function addCell(cell) {
}
function getCellAt(x,y) {
}
No comma needed, because these are not assignment statements, they are individual function definitions.
Upvotes: 4
Reputation: 1153
You are missing comma.
Board.prototype = {
addCell: function(cell) {
this.cells[getCellRepresentation(cell.x, cell.y)] = cell;
},
getCellAt: function(x,y) {
return this.cells[getCellRepresentation(x,y)]
}
}
Upvotes: 4