admin管理员组文章数量:1326327
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)]
}
}
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)]
}
}
Share
Improve this question
edited Jul 14, 2014 at 23:29
Daniel W.
32.4k15 gold badges99 silver badges155 bronze badges
asked Jul 14, 2014 at 23:25
desensitizeddesensitized
451 silver badge5 bronze badges
3
- Two missing semicolons :) – Gjermund B. Dahl Commented Jul 14, 2014 at 23:32
- 1 @gdahl—nope, one missing ma. – RobG Commented Jul 14, 2014 at 23:38
- That's true, and the reason why I gave the answer a +1. But the code example is also missing two semicolons, at least that's what my Netbeans told me, so as you see, my ment is not meant as an answer, as the question already was answered at the time of my ment, but merely meant as a ... ment. – Gjermund B. Dahl Commented Jul 15, 2014 at 8:59
2 Answers
Reset to default 4The first thing I see is that you are missing a ma:
Board.prototype = {
addCell: function(cell) {
this.cells[getCellRepresentation(cell.x, cell.y)] = cell;
}, // <---- put a ma here
getCellAt: function(x,y) {
return this.cells[getCellRepresentation(x,y)]
}
}
The reason you need a ma 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 ma needed, because these are not assignment statements, they are individual function definitions.
You are missing ma.
Board.prototype = {
addCell: function(cell) {
this.cells[getCellRepresentation(cell.x, cell.y)] = cell;
},
getCellAt: function(x,y) {
return this.cells[getCellRepresentation(x,y)]
}
}
本文标签: javascriptGetting an 39invalid return statement39 and I am not sure whyStack Overflow
版权声明:本文标题:javascript - Getting an 'invalid return statement' and I am not sure why - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742202087a2432149.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论