在jQuery中出现Cannot read property '0' of undefined错误

6
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<style>
td
{
    min-height: 50px;
    min-width: 50px;
}
</style>
</head>
<body>
<table border="1" id="tab">
<tr>
    <td class="game"></td>
    <td class="game"></td>
    <td class="game"></td>
    <td class="game"></td>
</tr>
<tr>
    <td class="game"></td>
    <td class="game"></td>
    <td class="game"></td>
    <td class="game"></td>
</tr>
<tr>
    <td class="game"></td>
    <td class="game"></td>
    <td class="game"></td>
    <td class="game"></td>
</tr>
<tr>
    <td class="game"></td>
    <td class="game"></td>
    <td class="game"></td>
    <td class="game"></td>
</tr>
</table>
<script>
    var tab=[4];
    $(document).ready(function()
    {
        var i,j;
        for(i=0;i<4;i++)
        {
            tab[i]=[4];
            for(j=0;j<4;j++)
            {
                tab[i][j]=null;
            }
        }
        randomnum();
    });
    function randomnum()
    {
        var num=Math.random();
        alert("called random num");
        if(num<0.5)
            num=2;
        else
            num=4;
        alert(num);
        var row=Math.floor(Math.random()*10);
        row=row%4;
        var col=Math.floor(Math.random()*10);
        col=col%4;
        while(tab[row][col]!=null)
        {
            var row=Math.random();
            row=(row*10)%4;
            var col=math.random();
            col=(col*10)%4;
            alert("random row col"+row+" "+col);
        }
        //alert("row:"+row+"col"+col);
        tab[row][col]=num;
        $("#tab tr:eq("+row+") td:eq("+col+")").text(num);
        keycheck();
    }
    function keycheck()
    {
        $(document).on("keydown",function(event){
            if(event.which==38)
                moveup();
            else if(event.which==40)
                movedown();
            else if(event.which==39)
                moveright();
            else if(event.which==37)
                moveleft();
            });
        }
    function moveup()
    {
        var row,col,j;
        for(col=0;col<4;col++)
        {
            for(row=0;row<3;row++)
            {
                if(tab[row][col]==tab[row+1][col])
                {
                    tab[row][col]=tab[row][col]*2;
                    row++;
                    tab[row][col]=null;
                }
            }
            for(row=0;row<3;row++)
            {
                for(j=row+1;j<4;j++)
                {
                    if (typeof j === "undefined") {
                        alert("j is undefined");
                    }
                    if (typeof row === "undefined") {
                        alert("col is undefined");
                    }
                    if (typeof col === "undefined") {
                        alert("col is undefined");
                    }
                    alert(j+" "+row+" "+col);
                    if(tab[j][col]==null&&tab[j+1][col]!=null)
                    {
                        tab[j][col]=tab[j+1][col];
                        tab[j+1][col]=null;
                        j++;
                    }
                }
            }
        }
        chntable();
    }
    function chntable()
    {
        var row,col;
        for(row=0;row<4;row++)
        {
            for(col=0;col<4;col++)
            {
                $("#tab tr:eq("+row+") td:eq("+col+")").text(num);
            }
        }
        randomnum();
    }
</script>
</body>
</html>

在上面的代码中,我遇到了

Cannot read property '0' of undefined

错误,它出现在moveup()中的if(tab[j+1][col]==null&&tab[j][col]!=null)。这个错误是什么,如何解决?从警告信息中,我看不到任何未定义的内容。那么是什么导致了这个问题?问题的原因是什么,我该如何避免?重复给出的内容是创建一个数组,但这并没有解决我的问题。JQuery库中的错误出现在dispatchq.handle处。


“tab” 只有一个键,即 “index(0)”,它保存了值 “4”。 - Rayon
@Rayon,为什么在其他选项卡调用时没有抛出错误,如何解决它? - Narayanasami N
@Rayon,在jQuery中创建静态的4X4数组还有其他方法吗? - Narayanasami N
为什么你把数组和jQuery联系起来?创建一个矩阵非常容易...我不明白你的代码想要实现什么... - Rayon
@Rayon 我正在尝试创建[2048游戏]http://2048game.com/。因此,我想要一个4X4的数组来存储值。 - Narayanasami N
可能是JavaScript多维数组的重复问题。 - Asons
1个回答

0

tab是一个数组。在这个数组中,你有对象。

无法读取未定义对象的属性'0'意味着你正在尝试读取一个未定义对象的属性->

var foo={bar:"test"}
console.log(foo[bar]); //you get "test"

在您的情况下:

var obj = tab[0] //obj is undefined
console.log(obj[0]); //you get "Cannot read property '0' of undefined"

如果你想检查,你可以轻松地使用以下代码检查每个对象:

if(tab[0]){
   //tab[0] is defined
     if(tab[0][0])
     {
       //tab[0][0] is defined
     }
}

有没有一种方法可以创建一个没有这个问题的4X4数组,或者有没有解决这个问题的方法。 - Narayanasami N
你需要检查 obj 是否未定义:if(tab[0]) {//tab[0] 已定义} - laren0815
我知道如何检查。我想知道如何解决这个问题,以便不会出现任何错误。 - Narayanasami N
如果(!tab[0]){tab[0]=randomNumber}... 如果(!tab[0][0]){tab[0][0]=otherRandomNumber} - laren0815

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接