访问属性时,Javascript对象变为未定义

访问属性时,Javascript对象变为未定义,javascript,html,Javascript,Html,我正在制作一个HTML5JavaScript游戏(不使用canvas),我需要一个对象类。该类是DPixel,所有对象都存储在一个名为grid的数组中。下面是课堂: var grid = new Array(); function DPixel(data, x, y, node) { this.xpos = x; this.ypos = y; this.elem = node; this.type = data[0]; /*The pixel will t

我正在制作一个HTML5JavaScript游戏(不使用canvas),我需要一个对象类。该类是DPixel,所有对象都存储在一个名为grid的数组中。下面是课堂:

var grid = new Array();
function DPixel(data, x, y, node) {
    this.xpos = x;
    this.ypos = y;
    this.elem = node;
    this.type = data[0];
    /*The pixel will transfer colors as normal*/
    if (this.type === "normal") {
        this.type = "normal";
        this.red = data[1];
        this.green = data[2];
        this.blue = data[3];
    }
    /*The pixel will not transfer colors to other pixels*/
    else if (this.type === "border") {
        this.type = "border";
        this.red = 224;
        this.green = 210;
        this.blue = 54;
    }
}
数组的实际填充在setup函数中,它确实会被调用,我已经测试过了,在那里一切都很好

函数设置(){
对于(var x=0;x

我从chrome上得到的错误是:“UncaughtTypeError:无法读取未定义的属性'type',在我的更新方法中,它每秒运行几次

function update() {
    console.log("update is working");
    //Update each DPixel
    var rotations = 0;
    for(var x = 0; x < gridSize; x++) {
        for(var y = 0; y < gridSize; y++) {
            var dpixel = grid[x * gridSize + y + 1];
            var pixelType = dpixel.type; //The error is here
            var red = grid[x * gridSize + y + 1].red;
            var green = grid[x * gridSize + y + 1].green;
            var blue = grid[x * gridSize + y + 1].blue;
            rotations++;
        }
    }
    $("#equalized").html(equalized);
    $("#equalPercent").html(  (equalized / (gridSize*gridSize))*100   );
    updateID = setTimeout(update, $("input[type='range']").val());
}
函数更新(){
日志(“更新正在工作”);
//更新每个DPixel
var旋转=0;
对于(var x=0;x

我在dpixel变量上尝试了console.log(),chrome像往常一样打印了它的所有属性。但是当我试图访问一个属性时,它会给我错误。这对某些人来说似乎很明显,但我花了几个小时调试时一定跳过了一些东西,所以不要害羞

我发现了小故障。和往常一样,这是我的弱点,数学。在定义
dpixel
和颜色变量的行中,我不需要向索引中添加1。默认情况下,我的数组已经存储了索引为0-224的变量,因此使用基于0的循环是可以的。
我怀疑最后一个元素给了我错误,但我无法证明它,尽管我记录了每个元素以检查它是否未定义。

你粘贴了一半代码。你在哪里定义了Equalized、gridSize和grid?这就是你的问题所在。你在哪里填充dpixel?您正试图从数组中获取它,但您实际在哪里创建它并将其推送到数组?很抱歉,忘记添加该部分。