Javascript 这个函数对生命游戏项目有什么作用

Javascript 这个函数对生命游戏项目有什么作用,javascript,Javascript,我正在做一个生活游戏项目。有人建议我创建自己的迭代函数,以便在需要迭代时使用它。以下是我在本节中的当前代码: var onCellClick = function (event) { // how to set the style of the cell when it's clicked if(this.className === "dead") { this.className = "alive"; } else { this.classNa

我正在做一个生活游戏项目。有人建议我创建自己的迭代函数,以便在需要迭代时使用它。以下是我在本节中的当前代码:

var onCellClick = function (event) {

    // how to set the style of the cell when it's clicked
    if(this.className === "dead") {
      this.className = "alive";
    } else {
      this.className = "dead";
    }
  };

  //applies the onclick listener to the entire board.
  this.iterateCells(function(cell, x, y) {
    cell.onclick = onCellClick;
  });

  GameOfLife.prototype.iterateCells = function(iterator) {
  for(var y=0; y < this.height; y++) {
    for(var x=0; x < this.width; x++) {
      var currentCell = document.getElementById(x+"-"+y);
      iterator(currentCell, x, y);
    }
  }
};
var onCellClick=函数(事件){
//如何设置单击单元格时的样式
if(this.className==“dead”){
this.className=“alive”;
}否则{
this.className=“dead”;
}
};
//将onclick侦听器应用于整个电路板。
iterateCells(函数(cell,x,y){
cell.onclick=onCellClick;
});
GameOfLife.prototype.iterateCells=函数(迭代器){
对于(变量y=0;y
我正在做的是为20/20网格中的每个单元格激活“onCellClick”功能

我不确定

a) 在iterate cell函数中,作为参数传入的迭代器是什么?它的价值是什么?这是我最困惑的部分

b) 当这个.iterateCells(函数(cell,x,y))被传递为迭代器参数的函数实际上在做什么


这里有一个指向该项目的链接,以防您需要了解更多正在发生的事情。

函数是JavaScript中的一流公民,这意味着您可以将它们存储在变量中并作为参数传递。例如:

// this function executes a function that is passed as a parameter
function execute(func)
{
    func();
}

function hello()
{
    alert("Hello, World!");
}

execute(hello);
execute(function () { alert("Hello again!"); });
此代码会提醒“你好,世界!”,然后再提醒“你好!”

具体回答您的问题:

a)
iterator
函数(cell,x,y){cell.onclick=onCellClick;}

b)
iterateCells

a)多次调用该函数,请参见。b) 不清楚-单元格是单元格对象,x和y是它所在的位置