Javascript 2D数组奇怪的行为

Javascript 2D数组奇怪的行为,javascript,arrays,matrix,Javascript,Arrays,Matrix,我使用javascript数组编写了一些代码,但我遇到了一个小问题 我正在运行为snake创建的init函数,但是输出对我来说有点奇怪。我肯定那是因为我是javascript新手。有人能向我解释发生了什么事吗?如何使代码生成所需的输出 var snake = { direction : null, head : null, queue : null, init: function(){ this.direction = RIGHT;

我使用javascript数组编写了一些代码,但我遇到了一个小问题

我正在运行为snake创建的init函数,但是输出对我来说有点奇怪。我肯定那是因为我是javascript新手。有人能向我解释发生了什么事吗?如何使代码生成所需的输出

var snake = {
    direction : null,
    head : null,
    queue : null,

    init: function(){
        this.direction = RIGHT;
        this.queue = []; // 2D ARRAY
        this.head = []; // 1D ARRAY

        this.insert([1,10]);
        this.insert([2,20]);
        this.insert([3,30]);
    },

    insert: function(x,y){
        this.queue.unshift([x,y]); // INSERTS [X,Y]
        this.head = this.queue[0];

        console.log(this.head + "T0"); // prints: 1,10 T0
        console.log(this.head[0] + " T1 "); // prints: 1,10 T1 
        console.log(this.head[1] + " T2 "); // prints: undefined T2 

        /*
            I was expecting (DESIRED OUTPUT):

            this.head to print 1,1

            this.head[0] to print 1

            this.head[1] to print 10

        */ 

    }

函数
insert
包含两个参数;一个用作数组中的第一个值,另一个用作要取消移位到
队列的第二个值。调用函数时,只传递一个参数(数组
[1,10]
),当取消移位时,该参数将用作第一个元素,第二个元素将不被定义


要解决问题,可以使用
this.insert(1,10)调用函数
或将函数更改为仅接受一个参数,然后
this.queue.unshift(x)

您的函数
insert
包含两个参数;一个用作数组中的第一个值,另一个用作要取消移位到
队列的第二个值。调用函数时,只传递一个参数(数组
[1,10]
),当取消移位时,该参数将用作第一个元素,第二个元素将不被定义


要解决问题,可以使用
this.insert(1,10)调用函数
或将函数更改为仅接受一个参数,然后
this.queue.unshift(x)

哦,我明白了。我错误地调用了函数。谢谢你的帮助。哦,我明白了。我错误地调用了函数。谢谢你的帮助。