Javascript 未定义函数错误
Javascript Undefined function error
我正在用JavaScript构建Conway's Game of Life。上一个问题已修复。........................................................................现在它说:无法读取在"for(var i =0; i<this.checkneighbors.length;>
//object constructor
function cell(){
this.alive = Math.random() > 0.7;
this.neighbours = 0; //number of live neighbours
this.checkneighbours = [[-1,-1],[-1,0],[0,-1],[-1,1],[1,-1],[1,0],[0,1],[1,1]];
}
function GoL(size){
this.size = size;
this.grid = this.makeGrid(size);
};
GoL.prototype.makeGrid = function(size){
var grid = [];
for(var i=0; i<size; i++){
var row=[];
for(var j =0; j<size; j++){
row.push(new cell());
}
grid.push(row);
}
return grid;
};
GoL.prototype.drawGrid = function(){
for(var i=0;i<this.size;i++){
var row =this.grid[i];
var rowCell="";
for(var j=0;j<this.size;j++){
var cell = row[j];
if(cell.alive){
rowCell += "X|";
}else{
rowCell += " |";
}
}
console.log(rowCell);
}
};
GoL.prototype.underpopulation = function(ro,col){
var cell = this.grid[ro][col];
if(cell.neighbours <2){
return true;
}else{
return false;
}
};
GoL.prototype.overpopulation = function(ro,col){
var cell = this.grid[ro][col];
if(cell.neighbours >3){
return true;
}else{
return false;
}
};
GoL.prototype.backtolife = function(ro,col){
var cell = this.grid[ro][col];
if(cell.neighbours ===3 && !cell.alive){
return true;
}else{
return false;
}
};
GoL.prototype.update = function(ro,col){
var cell = this.grid[ro][col];
cell.num_of_neighbours = 0;
for(var i =0; i<this.checkneighbours.length; i++){
var checkneighbour = this.checkneighbours[i];
var neighbour1 = direction[0];
var neighbour2 = direction[1];
if(neighbour1>=0 && neighbour1 < this.size && neighbour2 >=0 && neighbour2 < this.size){
var currentneighbour = this.grid[ro + neighbour1][col+neighbour2];
if(currentneighbour.alive){
cell.num_of_neighbours++;
}
}
}
};
GoL.prototype.updateAll = function(){
for(var i=0; i<this.size;i++){
for(var j=0; j<this.size;j++){
this.update(i,j);
}
}
}
GoL.prototype.cellstatus = function(ro,col){
var cell = this.grid[ro][col];
if(this.underpopulation(ro,col) || this.overpopulation(ro,col)){
cell.alive = false;
}else if(this.backtolife(ro,col)){
cell.alive = true;
}
};
GoL.prototype.allcellstatus = function(ro,col){
for(var i=0; i<this.size;i++){
for(var j=0; j<this.size;j++){
this.cellstatus(i,j);
}
}
};
var gameoflife = new GoL(40);
var interval = setInterval(function(){
GoL.drawGrid();
GoL.updateAll();
GoL.allcellstatus();
},200);
我想你的意思是
var gameoflife = new GoL(40);
var interval = setInterval(function(){
gameoflife.drawGrid();
gameoflife.updateAll();
gameoflife.allcellstatus();
},200);
应使用以下语法调用函数:
var gameoflife = new GoL(40);
var interval = setInterval(function(){
gameoflife.drawGrid();
gameoflife.updateAll();
gameoflife.allcellstatus();
},200);
您是在类上调用方法drawGrid
,而不是在实例上调用gameoflife
,而不是在实例本身的类上调用这些方法:
var interval = setInterval(function(){
gameoflife.drawGrid();
gameoflife.updateAll();
gameoflife.allcellstatus();
},200);
相关文章:
- XMLHttpRequest:需要使用ajax中的成功和错误函数
- Javascript 错误:函数预期
- 未捕获的引用错误:函数未使用 onchange 定义
- 如何让jQueryajax执行错误函数
- 未定义未捕获的引用错误函数
- $.ajax在跨域url上不返回错误函数
- jQuery.ajax+php5.3-始终执行错误函数
- javascript未捕获类型错误函数ajax
- 在 Promise 调用的错误函数回调后附加对象的用法是什么
- Ajax 请求在 HandleUnauthorizedRequest 之前命中错误函数
- 引用错误:“函数”未定义
- 黑莓 10 级联 ajax 调用从 javascript 总是执行错误函数
- Ajax 调用弹簧控制器回调错误函数
- 未定义未捕获的引用错误函数
- 主干.js - n捕获异常:语法错误:函数构造函数:无法编译函数
- 引用错误:函数未定义错误
- “未捕获的引用错误:(函数)未定义”与焦点事件
- JavaScript 未捕获的引用错误函数未定义 单击时
- 引用错误:函数未定义
- 类型错误: [函数名称] 不是函数,在猫鼬和节点.js中