gpt4 book ai didi

javascript - 在 Javascript 中,您可以在同一类的另一个函数中调用该类的函数吗?

转载 作者:行者123 更新时间:2023-12-03 11:19:45 25 4
gpt4 key购买 nike

编辑:我在 getNeighbor 函数中包含了更多详细信息

我知道下面的代码很长,但我已经尽可能地总结了。我有以下创建十六进制 map 的代码:

function Hex (col, row, ter) { //new class of Hexes with row, column, and terrain
this.col = col;
this.row = row;
this.ter = ter; }

function Map (num_col, num_row) { //create a map of Hexes of dimension num_col x num_row
this.num_col = num_col;
this.num_row = num_row;
this.hexArray = new Array();
for (i=0; i<num_col; i++) { //creates a 2D array of Hexes with "blank" terrain
this.hexArray[i] = new Array();
for (j=0; j<num_row; j++) {
this.hexArray[i][j] = new Hex(i, j, "blank"); }}

this.step1 = function() {
//assigns a terrain value to 30% of the hexes in the map ("seeds" the map) and returns an array called step1Array whose values are the seeded Hexes.

this.getNeighbor = function(hex) { //returns all the neighbors of the given hex in an array called neighborArray
var delta = [[1, -1, 0], [1, 0, -1], [0, 1, -1],
[-1, 1, 0], [-1, 0, 1], [0, -1, 1]];
this.neighborArray = new Array();

for (i=0; i<delta.length; i++) {
//get the coordinates of the neighbors and store them as newCol and newRow
if (newCol<0 || newRow<0 || newCol>this.num_col-1 || newRow>this.num_row-1 //skip any hexes that are out of bounds
|| this.hexArray[newCol][newRow].ter !== "blank") continue; //or that are already seeded
this.neighborArray.push(this.hexArray[newCol][newRow]); }

return this.neighborArray;
}

this.step2 = function() { //assigns a terrain value to all "blank" neighbors of the seeded hexes
for (i=0; i<this.step1Array.length; i++) {
this.getNeighbor(this.step1Array[i]);
if (this.neighborArray.length === 0) continue; //if there are no "blank" neighbors, continue to the next seeded Hex
else {
for (j=0; j<this.neighborArray.length; j++) { //else assign each blank neighbor a terrain value
var percent = Math.random();
var temp_col = this.neighborArray[i].col;
var temp_row = this.neighborArray[i].row;
//based on the value of "percent", assign this.hexArray[temp_col][temp_row].ter a given value
}
}
}
}

var testMap = new Map(3,3) //create a 3x3 map of hexes
testMap.step1(); //seed the map
testMap.step2(); //assign terrain values to neighbors of seeded hexes. get error.

我的问题是编译时出现以下错误:未捕获的类型错误:无法读取未定义的属性“col”

我对step1和getNeighbor函数进行了很多实验,它们返回准确的十六进制数组,所以我认为这些不是问题。

我不知道问题出在哪里。在step2函数中调用getNeighbor函数有问题吗?由于某种原因它不喜欢

var temp_col = this.neighborArray[i].col;

最佳答案

可能你的“this”并不是你在内部函数中所认为的“this”。

你可以尝试:

var _this = this;  //Create a local variable containing this
this.step2 = function() { //assigns a terrain value to all "blank" neighbors of the seeded hexes
for (i=0; i<this.step1Array.length; i++) {
_this.getNeighbor(this.step1Array[i]); //use _this instead of this
if (_this.neighborArray.length === 0) continue;

关于javascript - 在 Javascript 中,您可以在同一类的另一个函数中调用该类的函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27176067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com