为什么这个 for 循环不能正确读取传入的参数?

Why doesn't this for loop read this in passed argument properly?

下面是我的代码

function generateGrid(spacing, boundBox, geometry) {
    console.log(spacing);
    console.log(boundBox);
    var grid = [];
    console.log(spacing);
    for (var x = boundBox[0]; x < boundBox[2]; x = x + spacing) {
        for (var y = boundBox[1]; y < boundBox[3]; y = y + spacing) {
            if(geometry.intersectsCoordinate([x, y])) grid.push([x, y]);
            console.log(boundBox[3] - y)
        }
        console.log(boundBox[1] - x)
    }
    console.log(grid);
}

如果 spacing 被替换为类似 10000 的数字,for 循环执行得很好。

x 是一个数字,所以使用 String(x) 所以你在两个字符串之间使用运算符 +,这会给你 "15" + "1" = "151",但这可能不是你想要的

从您的控制台屏幕截图来看,传入的参数似乎是字符串 "10000" 而不是数字 10000

要么检查调用函数的代码,要么在函数内部转换为整数,例如使用 parseInt(spacing).

作为有助于在未来发现任何类似问题的提示,Chrome 的 console.log 以蓝色显示数值,以黑色显示字符串值。