最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting an 'invalid return statement' and I am not sure why - Stack Overflow

programmeradmin7浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 4

The 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)]
            }

}
发布评论

评论列表(0)

  1. 暂无评论