Javascript 获得';返回语句无效';我不知道为什么
我正在编写一个代码 我是JavaScript新手,我正在尝试向board对象添加一个方法,该方法将返回一个Javascript 获得';返回语句无效';我不知道为什么,javascript,Javascript,我正在编写一个代码 我是JavaScript新手,我正在尝试向board对象添加一个方法,该方法将返回一个单元格在board上的位置。但是我得到一个错误,告诉我这是一个无效的返回语句。你能解释一下我做错了什么吗 Board.prototype = { addCell: function(cell) { this.cells[getCellRepresentation(cell.x, cell.y)] = cell;
单元格在board上的位置。但是我得到一个错误,告诉我这是一个无效的返回语句
。你能解释一下我做错了什么吗
Board.prototype = {
addCell: function(cell) {
this.cells[getCellRepresentation(cell.x, cell.y)] = cell;
}
getCellAt: function(x,y) {
return this.cells[getCellRepresentation(x,y)]
}
}
你缺少逗号
Board.prototype = {
addCell: function(cell) {
this.cells[getCellRepresentation(cell.x, cell.y)] = cell;
},
getCellAt: function(x,y) {
return this.cells[getCellRepresentation(x,y)]
}
}
我看到的第一件事是您缺少一个逗号:
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)]
}
}
如果函数是普通命名函数,您可能会看到:
function addCell(cell) {
}
function getCellAt(x,y) {
}
不需要逗号,因为这些不是赋值语句,它们是单独的函数定义。缺少两个分号:)@gdahl nope,缺少一个逗号。这是真的,我给出答案的原因是a+1。但是代码示例也缺少两个分号,至少我的Netbeans告诉我的是这样的,所以正如您所看到的,我的评论并不是作为一个答案,因为问题在我发表评论时已经得到了回答,而只是作为一个。。。注释。您是指单个函数声明。;-)
function addCell(cell) {
}
function getCellAt(x,y) {
}