javascript - 如何使用 javascript 访问函数中传递的对象参数?

标签 javascript openlayers

如何使用 javascript 访问函数中传递的对象参数? 我想动态地使用各种 column_name 。 feature.attribute 有列名。我想将 feature.attributecolumn name 连接起来。到目前为止我已经尝试过:

我的代码:

var column_name = "LOCAL_POP";

var initialize = {
    init_style: function() {
        style = new OpenLayers.StyleMap({
            "default": new OpenLayers.Style(
                    {
                        fillColor: "${getColor}",
                        strokeColor: "#00FF01",
                        fillOpacity: 0.7,
                        graphicZIndex: 1,
                        label: "${DISTRICT}"
                    },
            {
                context: {
                    getColor: function(feature) {

**// tested by creating a local variable and window variable**

                        this.feature = feature;
                        var feature_name = 'feature.attributes.' + column_name;
                        console.log(window);
                        console.log(this['feature_name']);
                        console.log(window['feature_name']);
                        return  feature_name > 1000000 ? '#006D2C' :
                                feature_name > 100000 ? '#31A354' :
                                feature_name > 5000 ? '#74C476' :
                                feature_name >= 0 ? '#A1D99B' :
                                '';
                    }
                }
            }),
            "select": new OpenLayers.Style(
                    {
                        strokeColor: "#00FF01",
                        fillOpacity: 0.7
                    })
        });
        return style;
    }
};

最佳答案

您的属性(property)访问权有误。要动态访问对象的属性,请使用不带引号的方括号表示法(使其成为字符串而不是您想要的变量):

feature.attributes[column_name]

这是你的固定代码:

var column_name = "LOCAL_POP";

var initialize = {
    init_style: function() {
        style = new OpenLayers.StyleMap({
            "default": new OpenLayers.Style(
                    {
                        fillColor: "${getColor}",
                        strokeColor: "#00FF01",
                        fillOpacity: 0.7,
                        graphicZIndex: 1,
                        label: "${DISTRICT}"
                    },
            {
                context: {
                    getColor: function(feature) {

**// tested by creating a local variable and window variable**

                        this.feature = feature;
                        var feature_name = feature.attributes[column_name];
                        return  feature_name > 1000000 ? '#006D2C' :
                                feature_name > 100000 ? '#31A354' :
                                feature_name > 5000 ? '#74C476' :
                                feature_name >= 0 ? '#A1D99B' :
                                '';
                    }
                }
            }),
            "select": new OpenLayers.Style(
                    {
                        strokeColor: "#00FF01",
                        fillOpacity: 0.7
                    })
        });
        return style;
    }
};

关于javascript - 如何使用 javascript 访问函数中传递的对象参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19512137/

相关文章:

javascript - 夹线时插入阅读更多按钮

javascript - OpenLayers 上的滚轮缩放停止工作,div 设置为溢出 :auto

javascript - 如何将我的 CAD(DWG) 文件转换为 GeoJSON?

javascript - OpenLayers 2 : Unexpected results from Point. 距离()

postgresql - 如何将 Openlayers map 从 PostGIS 坐标居中?

javascript - GBK 编码/解码字符集

javascript - 为什么我的 HTML 元素 javascript 变量与我在 DOM 中看到的不匹配?

javascript - Three.js:自定义几何体不会被纹理化

asp.net - 通过 javascript 函数引用控件

zooming - OL 中 WMTS 瓷砖的锁定缩放