Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JS-拆分数组字符串并将这两部分作为参数传递给方法_Javascript_Arrays_String_Split - Fatal编程技术网

Javascript JS-拆分数组字符串并将这两部分作为参数传递给方法

Javascript JS-拆分数组字符串并将这两部分作为参数传递给方法,javascript,arrays,string,split,Javascript,Arrays,String,Split,我有一个从x,y坐标中获取特定DIV的方法: this.getCell = function (x, y){ this.index = x + y * this.width; return this.cells[this.index]; } 我想将我的方法用于另一个: this.computeCellNextState = function(x, y){ var nearbies = ['x-1,y-1','x,y-1','x+1,y-1'];

我有一个从x,y坐标中获取特定DIV的方法:

this.getCell = function (x, y){
      this.index = x + y * this.width;
      return this.cells[this.index];
}
我想将我的方法用于另一个:

this.computeCellNextState = function(x, y){

      var nearbies = ['x-1,y-1','x,y-1','x+1,y-1'];
      var splitter = nearbies[0].split(',');

      console.log(this.getCell(splitter[0],splitter[1])); // returns undefined

}
我想要达到的目标:

this.getCell(x-1,y-1)

x-1,y-1 are from nearbies[0]

我想拆分一个“nearbies”字符串并用作两个参数。

'x-1,y-1'
等只是字符串,它们对
getCell
没有任何意义。您必须在
computeNextState
中使用实际表达式,例如

this.computeCellNextState = function(x, y){

      var nearbies = [[x-1,y-1],[x,y-1],[x+1,y-1]];

      console.log(this.getCell(...nearbies[0]))

}

这就是我错过的,菜鸟的错误。谢谢您!