javascript - 为什么这个 for 循环不能正确读取传递的参数中的 this ?

标签 javascript openlayers-3

下面是我的代码

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);
}

console output

如果将 spacing 替换为 10000 之类的数字,则 for 循环可以正常执行。

最佳答案

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

检查调用函数的代码,或者在函数内转换为整数,例如使用 parseInt(spacing)

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

关于javascript - 为什么这个 for 循环不能正确读取传递的参数中的 this ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41768577/

相关文章:

javascript - 将mapbox-style转换为openlayers 3 vectorTile样式

javascript - BNG 中的 Open Layers 3 鼠标位置检测不起作用

javascript - 在文件夹中搜索文件名的部分内容

javascript - 如何设置 Javascript 片段的样式

javascript - 有什么方法可以覆盖 Angular 数据列表自动完成行为吗?

javascript - 当区域重复时如何正确从 Open Layers v4.2、API 获取坐标

openlayers-3 - 如何在 OpenLayers 3 中注册 map 移动/ map 平移事件

javascript对象属性/数组操作

javascript - 使用函数angularJS在ng repeat中返回增量ng repeat

javascript - openlayers3 loadingstrategy jsonp readFeatures 给出 'undefined is not a function'