Javascript:console.log(arr.length)表示长度为n,console.log(arr)表示长度为n-1。第二个案例不会显示最后一个元素

时间:2014-05-04 11:30:52

标签: javascript arrays algorithm object a-star

第一行将打印,例如,10,而第二行将打印少一行:

console.log("ds length: " + ds.length);  // Prints, say, 10
console.log(ds); // Prints all but last element and says length is one less.

我的代码的一些相关部分,都在单例AS中:

FindPath: function(oX, oY, dX, dY) { // origin, destination
        var heap = AS.Heap();

        var g = AS.Grid; // AS.Grid is declared, in AS, as: Grid: new Array(),

        var node;

        var cn = g[oX][oY]; // current node
        var dn = g[dX][dY]; // destination node

        heap.push(cn);

        cn.CoorType = AS.CoorType.VISITED;

        while (heap.length > 0) {
            cn = heap.pop();

            if (cn === dn) {
                var ds = new Array(); // direction set (instructions)
                var pn;// parent node;

                do {
                    pn = cn.Parent;
                    ds.push({
                        Dir: cn.Y - pn.Y !== 0 ? cn.Y - pn.Y < 0 ? AS.Direction.UP : AS.Direction.DOWN : cn.X - pn.X > 0 ? AS.Direction.RIGHT : AS.Direction.LEFT,
                        Node: cn});
                    cn = pn;
                } while (pn.Parent);

                console.log("ds length: " + ds.length);
                console.log(ds);

                AS.CleanUp();
                return ds;
            }

                    // Bellow, I'm not using functions 'cause occasionally, when FindPath() is called multiple times in a large area within a few milliseconds, it will get laggy. I removed the use of functions to increase performance.
            node = g[cn.X+1][cn.Y];
            if (node.CoorType === AS.CoorType.NOTHING) {
                node.CoorType = AS.CoorType.VISITED;
                node.Parent = cn;
                node.Score = ((Math.abs(node.X - oX) + Math.abs(node.Y - oY)) * AS.SMALL_ADVANTAGE + Math.abs(node.X - dX) + Math.abs(node.Y - dY)) +
                        Math.abs((node.X - dX) * (oY - dY) - (node.Y - dY) * (oX - dX)) * 0.0001;
                heap.Sink(node);
            }
            node = g[cn.X][cn.Y+1];
            if (node.CoorType === AS.CoorType.NOTHING) {
                node.CoorType = AS.CoorType.VISITED;
                node.Parent = cn;
                node.Score = ((Math.abs(node.X - oX) + Math.abs(node.Y - oY)) * AS.SMALL_ADVANTAGE + Math.abs(node.X - dX) + Math.abs(node.Y - dY)) +
                        Math.abs((node.X - dX) * (oY - dY) - (node.Y - dY) * (oX - dX)) * 0.0001;
                heap.Sink(node);
            }
            node = g[cn.X-1][cn.Y];
            if (node.CoorType === AS.CoorType.NOTHING) {
                node.CoorType = AS.CoorType.VISITED;
                node.Parent = cn;
                node.Score = ((Math.abs(node.X - oX) + Math.abs(node.Y - oY)) * AS.SMALL_ADVANTAGE + Math.abs(node.X - dX) + Math.abs(node.Y - dY)) +
                        Math.abs((node.X - dX) * (oY - dY) - (node.Y - dY) * (oX - dX)) * 0.0001;
                heap.Sink(node);
            }
            node = g[cn.X][cn.Y-1];
            if (node.CoorType === AS.CoorType.NOTHING) {
                node.CoorType = AS.CoorType.VISITED;
                node.Parent = cn;
                node.Score = ((Math.abs(node.X - oX) + Math.abs(node.Y - oY)) * AS.SMALL_ADVANTAGE + Math.abs(node.X - dX) + Math.abs(node.Y - dY)) +
                        Math.abs((node.X - dX) * (oY - dY) - (node.Y - dY) * (oX - dX)) * 0.0001;
                heap.Sink(node);
            }
        }

        AS.CleanUp();
        return heap; // No path found
    },

Heap: function() {
    var heap = new Array();

    heap.Sink = function(node) {
        var i = this.length-1;

        while (i > 0 && this[i].Score < node.Score) i--;

        this.splice(i, 0, node);
    };

    return heap;
},

CleanUp: function() { // Cleans up changes made by any previous path search
    var x, y, g = AS.Grid;

    var limX = g.length - 3;
    var limY = g[0].length - 3;

    for (x = 2; x < limX; x++) {
        for (y = 2; y < limY; y++) {
            if (g[x][y].CoorType === AS.CoorType.VISITED) {
                g[x][y].CoorType = AS.CoorType.NOTHING;
                delete g[x][y].Parent;
            }
        }
    }
}

我意识到有时候,根据FindPath返回的路径移动的对象将错过一步。然后我看到了上面描述的内容。

enter image description here

1 个答案:

答案 0 :(得分:1)

在do {} while(pn.Parent)中添加两行:

do {
    pn = cn.Parent;
    ds.push({
        Dir: cn.Y - pn.Y !== 0 ? cn.Y - pn.Y < 0 ? AS.Direction.UP : AS.Direction.DOWN : cn.X - pn.X > 0 ? AS.Direction.RIGHT : AS.Direction.LEFT,
        Node: cn});
    console.log(ds.length); // Added this line,
    console.log(ds);        // and this one
    cn = pn;
} while (pn.Parent);

我意识到console.log(ds)在调用GameLoop()之后打印了ds,它在某些时候会调用AS.FindPAth,而console.log(ds.length)正在打印ds当我要求打印时,它的长度就像我一样。

因为,在GameLoop()结束之前,我正在删除我的FindPath()返回的路径的最后一个元素(nextDirection = path.pop()),console.log(ds)将打印自己缺少一个元素。

尽管如此,javascript在执行诸如打印对象状态之类的事情时会更加坚定,因为它将在以后发生,而不是在console.log()时... ...