在javascript变量声明中混合逗号和分号的效果

时间:2016-08-24 06:04:28

标签: javascript

在下面的代码中,horiz被声明,然后运行for循环,用空数组填充它。对于verti在第二行上相同等。"var"声明是否仅适用于horiz(即分号打破"var"声明)或"var"声明还初始化horizvertiherepathunvisited

var horiz =[]; for (var j= 0; j<x+1; j++) horiz[j]= [],
    verti =[]; for (var j= 0; j<x+1; j++) verti[j]= [],
    here = [Math.floor(Math.random()*x), Math.floor(Math.random()*y)],
    path = [here],
    unvisited = [];

编辑:为清晰起见,此处添加了完整功能。函数中的变量不是全局的,或者在函数外部进行初始化。

编辑2:代码来自此处:http://rosettacode.org/wiki/Maze_generation#JavaScript

function maze(x,y) {
    var n=x*y-1;
    if (n<0) {alert("illegal maze dimensions");return;}
    var horiz =[]; for (var j= 0; j<x+1; j++) horiz[j]= [],
        verti =[]; for (var j= 0; j<x+1; j++) verti[j]= [],
        here = [Math.floor(Math.random()*x), Math.floor(Math.random()*y)],
        path = [here],
        unvisited = [];
    for (var j = 0; j<x+2; j++) {
        unvisited[j] = [];
        for (var k= 0; k<y+1; k++)
            unvisited[j].push(j>0 && j<x+1 && k>0 && (j != here[0]+1 || k !=     here[1]+1));
    }
    while (0<n) {
        var potential = [[here[0]+1, here[1]], [here[0],here[1]+1],
            [here[0]-1, here[1]], [here[0],here[1]-1]];
        var neighbors = [];
        for (var j = 0; j < 4; j++)
            if (unvisited[potential[j][0]+1][potential[j][1]+1])
                neighbors.push(potential[j]);
        if (neighbors.length) {
            n = n-1;
            next= neighbors[Math.floor(Math.random()*neighbors.length)];
            unvisited[next[0]+1][next[1]+1]= false;
            if (next[0] == here[0])
                horiz[next[0]][(next[1]+here[1]-1)/2]= true;
            else 
                verti[(next[0]+here[0]-1)/2][next[1]]= true;
            path.push(here = next);
        } else 
            here = path.pop();
    }
    return {x: x, y: y, horiz: horiz, verti: verti};
}

2 个答案:

答案 0 :(得分:1)

var horiz =[]; //variable declaration

for (var j= 0; j<x+1; j++) //for loop start
  horiz[j]= [], verti =[]; //variable declaration 
//for loop end


for (var j= 0; j<x+1; j++) //for loop start
    verti[j]= [], here = [Math.floor(Math.random()*x), Math.floor(Math.random()*y)], path = [here], unvisited = []; //variable declaration
//for loop end

由于缺少大括号,你可能会感到困惑

var horiz =[]; //variable declaration

for (var j= 0; j<x+1; j++) {
  horiz[j]= [], verti =[]; //variable declaration
}


for (var j= 0; j<x+1; j++) {
    verti[j]= [], here = [Math.floor(Math.random()*x), Math.floor(Math.random()*y)], path = [here], unvisited = []; //variable declaration
}

答案 1 :(得分:0)

您的代码有几个错误,您应该检查控制台以查看它们。因此,您的变量未声明 - 仅声明horizvar horiz =[]之后的分号将结束其他变量的声明。所以这段代码:

var horiz =[]; for (var j= 0; j<x+1; j++) horiz[j]= [],
    verti =[]; for (var j= 0; j<x+1; j++) verti[j]= [],
    here = [Math.floor(Math.random()*x), Math.floor(Math.random()*y)],
    path = [here],
    unvisited = [];

更改为:

var horiz = [],
    verti = [],
    here = [Math.floor(Math.random()*x), Math.floor(Math.random()*y)],
    path = [here],
    unvisited = [];

for (var j = 0; j < x + 1; j++) {
    horiz[j] = [];
}

for (var j = 0; j < x + 1; j++) {
    verti[j] = [];
}

另外,我建议您使用大括号{ ... }以提高可读性。